本文介紹了使用HttpURLConnection執(zhí)行cURL命令的Java返回204(HTTP_NO_CONTENT)的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!
問(wèn)題描述
我使用的是Java 11。在命令行上執(zhí)行時(shí),我有以下cURL命令:
curl --location --request GET 'http://xxx.xxx.co.za:8080/document/details/Select+docId+From+%27Workflow+Club%2FCustomer+Invoices%27+where+recursive+%3D+true+and+invoice_number+%3D%271221669023%27' --header 'Authorization: Basic xxx'
返回以下內(nèi)容:
{errorMessage: 'PaperTrail API only available in enterprise edition'}
但是,當(dāng)我嘗試使用HttpURLConnection
在Java應(yīng)用程序中執(zhí)行相同的URL時(shí),它返回空白響應(yīng)。
private static final String USER_AGENT = "Mozilla/5.0";
private static final String GET_URL = "http://xxx.xxx.co.za:8080/document/details/";
private static final String GET_URL_QRY = "Select docId From 'Workflow Club/Customer Invoices' where recursive = true and invoice_number =':1'";
private static final String GET_AUTH_ENC = "Basic xxx";
@Override
public String getDocId(Long invoiceNumber) {
String get_url_qry = StringUtils.replace(GET_URL_QRY, ":1", Long.toString(invoiceNumber));
get_url_qry = URLEncoder.encode(get_url_qry, StandardCharsets.UTF_8);
final String get_url = GET_URL+get_url_qry;
try {
URL url = new URL(get_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Authorization", GET_AUTH_ENC);
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
int responseCode = con.getResponseCode();
logger.info(get_url+" -> GET Response Code :: " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) { // success
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
String resp = response.toString();
logger.info(responseCode+" Response: '"+resp+"'.");
} else {
logger.error("GET request did not work (responseCode: "+responseCode+").");
}
} catch (MalformedURLException e) {
logger.error("MalformedURLException creating URL '"+get_url+"'. "+e.getMessage());
} catch (IOException e) {
logger.error("IOException creating connection from URL '"+get_url+"'. "+e.getMessage());
}
return null;
}
以空白響應(yīng)輸出以下內(nèi)容:
204 Response: ''.
問(wèn)題
如何使Java應(yīng)用程序也返回與命令行調(diào)用相同的結(jié)果?
更新
我有一個(gè)不同的POST URL,我也需要調(diào)用它,我可以成功地調(diào)用它。所以我的Get調(diào)用有問(wèn)題。
private static final String USER_AGENT = "Mozilla/5.0";
例如,Get調(diào)用返回204,沒(méi)有內(nèi)容。
private String getDocId(Long invoiceNumber) {
String get_url_qry = StringUtils.replace(GET_URL_QRY, ":1", Long.toString(invoiceNumber));
get_url_qry = URLEncoder.encode(get_url_qry, StandardCharsets.UTF_8);
final String get_url = GET_URL+get_url_qry;
try {
URL url = new URL(get_url);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Authorization", GET_AUTH_ENC);
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
Map<String,String> data = handleResponse(con);
return data.get("docId");
} catch (MalformedURLException e) {
logger.error("MalformedURLException creating URL '"+get_url+"'. "+e.getMessage());
} catch (IOException e) {
logger.error("IOException creating connection from URL '"+get_url+"'. "+e.getMessage());
}
return null;
}
返回200的POST調(diào)用和預(yù)期內(nèi)容。
private String getDocLink(String docId) {
if (StringUtils.isNotBlank(docId)) {
try {
URL url = new URL(POST_URL);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("Authorization", GET_AUTH_ENC);
con.setRequestMethod("POST");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
byte[] postDataBytes = getPostData(docId);
con.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
con.setDoOutput(true);
con.getOutputStream().write(postDataBytes);
Map<String,String> data = handleResponse(con);
return data.get("url");
} catch (IOException e) {
logger.error("IOException creating connection from URL '"+POST_URL+"'. "+e.getMessage());
}
} else {
logger.error("No docId provided when trying to get a document link.");
}
return null;
}
所以看到POST調(diào)用起作用了,我想我一定是GET調(diào)用出了什么問(wèn)題。
推薦答案
您是否嘗試在您的Java代碼中設(shè)置cURL將使用的相同用戶代理?類似curl/7.37.0
的內(nèi)容?
就我所知,這應(yīng)該就是所有的不同之處。在重定向之后,撇開(kāi)卷曲。但由于沒(méi)有重定向,我猜可能是用戶代理起了作用。
有很多服務(wù)器應(yīng)用程序在被瀏覽器調(diào)用時(shí)表現(xiàn)不同(就像您通過(guò)將User-Agent設(shè)置為Mozilla/5.0
來(lái)使其思考一樣),而不是像cURL這樣的其他應(yīng)用程序。
這篇關(guān)于使用HttpURLConnection執(zhí)行cURL命令的Java返回204(HTTP_NO_CONTENT)的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,