我正在使用 Java 11。當我在命令列上執行時,我有以下 curl 命令:
curl --location --request GET 'http://xxx.xxx.co.za:8080/document/details/Select docId From 'Workflow Club/Customer Invoices' where recursive = true and invoice_number ='1221669023'' --header 'Authorization: Basic xxx'
回傳以下內容:
{errorMessage: 'PaperTrail API only available in enterprise edition'}
但是,當我嘗試在 Java 應用程式中使用 執行相同的 URL 時HttpURLConnection,它回傳一個空白回應。
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;
}
以空白回應輸出以下內容:
204 Response: ''.
問題
如何讓 Java 應用程式也回傳與命令列呼叫相同的內容?
更新
我有一個不同的 POST url,我也需要呼叫它,我可以成功呼叫它。所以我的 GET 呼叫有問題。
private static final String USER_AGENT = "Mozilla/5.0";
例如,回傳 204 且沒有內容的 GET 呼叫。
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;
}
POST 呼叫,回傳 200 和預期的內容。
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 呼叫有效,我想我一定是 GET 呼叫有問題。
uj5u.com熱心網友回復:
您是否嘗試過,在您的 Java 代碼中設定相同的用戶代理,cURL 會使用?像curl/7.37.0什么?據我所知,這應該是全部,有什么不同。除了 cURL 跟隨重定向。但是由于沒有重定向,我想可能是用戶代理有所作為。
有很多服務器應用程式在被瀏覽器呼叫時表現不同(就像您通過將 User-Agent 設定為Mozilla/5.0),而不是其他一些應用程式,例如 cURL。
uj5u.com熱心網友回復:
根據您的描述,原始呼叫會產生錯誤。
假設您在 GET 的 java 代碼中也遇到某種形式的錯誤,您將捕獲例外并簡單地記錄它。然后你會回傳null而不是字串,這會導致你的回應沒有內容,即204。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/378709.html
