HttpClient的HttpResponse返回未格式化的字符串。

huangapple go评论78阅读模式
英文:

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 + &quot;:&quot; + password;
byte[] encodedAuth = Base64.encodeBase64(auth.getBytes());
String authHeader = &quot;Basic &quot; + new String(encodedAuth);
requestPost.setHeader(HttpHeaders.AUTHORIZATION, authHeader);
requestPost.setHeader(&quot;Content-Type&quot;, &quot;application/x-www-form-urlencoded&quot;);
// Create the request parameter body
List&lt;BasicNameValuePair&gt; urlParameters = new ArrayList&lt;&gt;();
urlParameters.add(new BasicNameValuePair(&quot;functionName&quot;, functionName));
urlParameters.add(new BasicNameValuePair(&quot;objectType&quot;, objectType));
urlParameters.add(new BasicNameValuePair(&quot;inputParameter&quot;, inputParameter));
urlParameters.add(new BasicNameValuePair(&quot;inputTableParameter&quot;, inputTableParameter));
// Execute request
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(urlParameters);
requestPost.setEntity(entity);
// bypass SSL security
SSLContext context = SSLContext.getInstance(&quot;TLSv1.2&quot;);
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(), &quot;UTF-8&quot;);
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:

&lt;OUTPUT&gt;
&lt;EN_ERROR_CD&gt;00&lt;/EN_ERROR_CD&gt;
&lt;ES_APPLICATION&gt;
&lt;EFFECTIVE_DT&gt;2020-07-31&lt;/EFFECTIVE_DT&gt;
&lt;JOURNALSTAT_CD&gt;100&lt;/JOURNALSTAT_CD&gt;
&lt;JOURNALSTAT_TT&gt;In Process&lt;/JOURNALSTAT_TT&gt;
&lt;APPL_DT&gt;2020-07-31&lt;/APPL_DT&gt;
&lt;APPLIN_DT&gt;2020-07-31&lt;/APPLIN_DT&gt;
&lt;MOD_DT&gt;2020-07-31&lt;/MOD_DT&gt;
&lt;POLICYNR_TT&gt;00002210000825255&lt;/POLICYNR_TT&gt;
&lt;BIZPROCSTAT_CD&gt;003&lt;/BIZPROCSTAT_CD&gt;
&lt;BIZPROCSTAT_TT&gt;Calculated&lt;/BIZPROCSTAT_TT&gt;
&lt;/ES_APPLICATION&gt;
&lt;ES_POLICY&gt;
&lt;POLICYNR_TT&gt;00002210000825255&lt;/POLICYNR_TT&gt;
&lt;POLICYNROLD_TT&gt;&lt;/POLICYNROLD_TT&gt;
&lt;EXCHANGERATE_TP&gt;&lt;/EXCHANGERATE_TP&gt;
&lt;CURRENCY_ID&gt;EUR&lt;/CURRENCY_ID&gt;
&lt;CURRENCY_TT&gt;Euro&lt;/CURRENCY_TT&gt;
&lt;SALECH_CD&gt;001&lt;/SALECH_CD&gt;
&lt;POLBEG_DT&gt;2020-07-31&lt;/POLBEG_DT&gt;
&lt;STARTTIME_TM&gt;00:00:00&lt;/STARTTIME_TM&gt;
...etc

For reference this is how my CURL request looks like:

curl -u username:password -d &quot;functionName=/RETRIEVE&amp;objectType=obj&amp;inputParameter=&lt;INPUT&gt;&lt;IV_APPLNR_CD&gt;10000000002553402&lt;/IV_APPLNR_CD&gt;&lt;IS_REQUESTED_ELEMENTS&gt;&lt;/IS_REQUESTED_ELEMENTS&gt;&lt;IF_READ_JOURNAL&gt;&lt;/IF_READ_JOURNAL&gt;&lt;IF_READ_CBC&gt;X&lt;/IF_READ_CBC&gt;&lt;/INPUT&gt;&amp;inputTableParameter=&quot; -H &quot;Content-Type: application/x-www-form-urlencoded&quot; -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, &quot;true&quot;);

huangapple
  • 本文由 发表于 2020年8月6日 03:21:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/63272185.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定