英文:
HttpClient's HttpResponse returns non formatted String
问题
I have created a post request which is executed using the HttpClient. This returns me a response, and from that I retrieve the entity, and convert it to a string to read the response value. The response contains the correct data, but it loses all relevant XML tags, and contains only the data. However, when I make this exact post request through CURL or Postman, the response contains the XML tag. I am unable to figure out what I am doing wrong. This is what I have tried:
// Create HTTP Post Request
HttpPost requestPost = new HttpPost(url);
// Create Auth
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes());
String authHeader = "Basic " + new String(encodedAuth);
requestPost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
requestPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
// Create the request parameter body
List<BasicNameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("functionName", functionName));
urlParameters.add(new BasicNameValuePair("objectType", objectType));
urlParameters.add(new BasicNameValuePair("inputParameter", inputParameter));
urlParameters.add(new BasicNameValuePair("inputTableParameter", inputTableParameter));
// Execute request
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(urlParameters);
requestPost.setEntity(entity);
// bypass SSL security
SSLContext context = SSLContext.getInstance("TLSv1.2");
TrustManager[] trustManager = new TrustManager[] {
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(X509Certificate[] certificate, String str) {}
@Override
public void checkServerTrusted(X509Certificate[] certificate, String str) {}
}
};
context.init(null, trustManager, new SecureRandom());
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpClient client = HttpClientBuilder.create().setSSLSocketFactory(socketFactory).build();
HttpResponse response = client.execute(requestPost);
//Get response
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
return result;
Without posting all the results, my final result looks something like this:
Results 002020-07-31100In Process2020-07-312020-07-312020-07-3100002210000825255003Calculated00002210000825255EUREuro0012020-07-3100:00:00
... etc
But from postman or CURL it is nicely formatted with the XML tags:
<OUTPUT>
<EN_ERROR_CD>00</EN_ERROR_CD>
<ES_APPLICATION>
<EFFECTIVE_DT>2020-07-31</EFFECTIVE_DT>
<JOURNALSTAT_CD>100</JOURNALSTAT_CD>
<JOURNALSTAT_TT>In Process</JOURNALSTAT_TT>
<APPL_DT>2020-07-31</APPL_DT>
<APPLIN_DT>2020-07-31</APPLIN_DT>
<MOD_DT>2020-07-31</MOD_DT>
<POLICYNR_TT>00002210000825255</POLICYNR_TT>
<BIZPROCSTAT_CD>003</BIZPROCSTAT_CD>
<BIZPROCSTAT_TT>Calculated</BIZPROCSTAT_TT>
</ES_APPLICATION>
<ES_POLICY>
<POLICYNR_TT>00002210000825255</POLICYNR_TT>
<POLICYNROLD_TT></POLICYNROLD_TT>
<EXCHANGERATE_TP></EXCHANGERATE_TP>
<CURRENCY_ID>EUR</CURRENCY_ID>
<CURRENCY_TT>Euro</CURRENCY_TT>
<SALECH_CD>001</SALECH_CD>
<POLBEG_DT>2020-07-31</POLBEG_DT>
<STARTTIME_TM>00:00:00</STARTTIME_TM>
...etc
For reference this is how my CURL request looks like:
curl -u username:password -d "functionName=/RETRIEVE&objectType=obj&inputParameter=<INPUT><IV_APPLNR_CD>10000000002553402</IV_APPLNR_CD><IS_REQUESTED_ELEMENTS></IS_REQUESTED_ELEMENTS><IF_READ_JOURNAL></IF_READ_JOURNAL><IF_READ_CBC>X</IF_READ_CBC></INPUT>&inputTableParameter=" -H "Content-Type: application/x-www-form-urlencoded" -X POST https://hostname:port/test/service/service.do
I hope this information is helpful in illustrating the problem. Thank you very much for your help!
英文:
I have created a post request which is executed using the HttpClient. This returns me a response, and from that I retrieve the entity, and convert it to a string to read the response value. The response contains the correct data, but it loses all relevant XML tags, and contains only the data. However, when I make this exact post request through CURL or Postman, the response contains the XML tag. I am unable to figure out what I am doing wrong. This is what I have tried:
// Create HTTP Post Request
HttpPost requestPost = new HttpPost(url);
// Create Auth
String auth = username + ":" + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes());
String authHeader = "Basic " + new String(encodedAuth);
requestPost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
requestPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
// Create the request parameter body
List<BasicNameValuePair> urlParameters = new ArrayList<>();
urlParameters.add(new BasicNameValuePair("functionName", functionName));
urlParameters.add(new BasicNameValuePair("objectType", objectType));
urlParameters.add(new BasicNameValuePair("inputParameter", inputParameter));
urlParameters.add(new BasicNameValuePair("inputTableParameter", inputTableParameter));
// Execute request
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(urlParameters);
requestPost.setEntity(entity);
// bypass SSL security
SSLContext context = SSLContext.getInstance("TLSv1.2");
TrustManager[] trustManager = new TrustManager[] {
new X509TrustManager() {
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
@Override
public void checkClientTrusted(X509Certificate[] certificate, String str) {}
@Override
public void checkServerTrusted(X509Certificate[] certificate, String str) {}
}
};
context.init(null, trustManager, new SecureRandom());
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
HttpClient client = HttpClientBuilder.create().setSSLSocketFactory(socketFactory).build();
HttpResponse response = client.execute(requestPost);
//Get response
String result = EntityUtils.toString(response.getEntity(), "UTF-8");
return result;
Without posting all the results, my final result looks something like this:
Results 002020-07-31100In Process2020-07-312020-07-312020-07-3100002210000825255003Calculated00002210000825255EUREuro0012020-07-3100:00:00
... etc
But from postman or CURL it is nicely formatted with the XML tags:
<OUTPUT>
<EN_ERROR_CD>00</EN_ERROR_CD>
<ES_APPLICATION>
<EFFECTIVE_DT>2020-07-31</EFFECTIVE_DT>
<JOURNALSTAT_CD>100</JOURNALSTAT_CD>
<JOURNALSTAT_TT>In Process</JOURNALSTAT_TT>
<APPL_DT>2020-07-31</APPL_DT>
<APPLIN_DT>2020-07-31</APPLIN_DT>
<MOD_DT>2020-07-31</MOD_DT>
<POLICYNR_TT>00002210000825255</POLICYNR_TT>
<BIZPROCSTAT_CD>003</BIZPROCSTAT_CD>
<BIZPROCSTAT_TT>Calculated</BIZPROCSTAT_TT>
</ES_APPLICATION>
<ES_POLICY>
<POLICYNR_TT>00002210000825255</POLICYNR_TT>
<POLICYNROLD_TT></POLICYNROLD_TT>
<EXCHANGERATE_TP></EXCHANGERATE_TP>
<CURRENCY_ID>EUR</CURRENCY_ID>
<CURRENCY_TT>Euro</CURRENCY_TT>
<SALECH_CD>001</SALECH_CD>
<POLBEG_DT>2020-07-31</POLBEG_DT>
<STARTTIME_TM>00:00:00</STARTTIME_TM>
...etc
For reference this is how my CURL request looks like:
curl -u username:password -d "functionName=/RETRIEVE&objectType=obj&inputParameter=<INPUT><IV_APPLNR_CD>10000000002553402</IV_APPLNR_CD><IS_REQUESTED_ELEMENTS></IS_REQUESTED_ELEMENTS><IF_READ_JOURNAL></IF_READ_JOURNAL><IF_READ_CBC>X</IF_READ_CBC></INPUT>&inputTableParameter=" -H "Content-Type: application/x-www-form-urlencoded" -X POST https://hostname:port/test/service/service.do
I hope this information is helpful in illustrating the problem. Thank you very much for your help!
答案1
得分: 1
已成功修复问题,通过在创建 POST 请求时添加以下代码行:
requestPost.setHeader(HttpHeaders.ACCEPT_CHARSET, "true");
英文:
Was able to fix the problem by adding this line when creating the post request:
requestPost.setHeader(HttpHeaders.ACCEPT_CHARSET, "true");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论