使用HttpClient4.5.2去GET一个网站的数据,得到了一堆加密后的数据。得到了如何解析出有用的不带加密的网站返回数据?这个花了不少的时间搜索和研究。罗列如下:
设置Header里面的加密(httpget.addHeader("Accept-Encoding", "gzip,deflate");)。如何设置干脆成不加密传输,还没有解决,待进一步研究。
关键是使用EntityUtils.toString(entity3)来把entity给变成字符串才是对的。其他的方法,不能得出正确答案,只能得到一些类似: org.apache.http.client.entity.GzipDecompressingEntity@129a250c:的返回。
具体相关代码如下所示:
HttpGet httpget = new HttpGet("http://utilitytunnel.ewei.com/api/v1/engineers/16317/tickets.json?_count=0");
httpget.addHeader("Content-Type", "application/json;charset=UTF-8");
httpget.addHeader("Accept-Encoding", "gzip,deflate");
httpget.setHeader("Accept", "application/json");
httpget.addHeader("_app_key", appKey);
httpget.addHeader("_timestamp", timestampString);
httpget.addHeader("_sign", sign);
// httpget.addHeader("searchKey", "齐晓东");
System.out.println("GET="+httpget.toString());
Header headers[] = httpget.getAllHeaders();
for (Header header:headers){
System.out.println(header.getName()+" "+header.getValue());
}
CloseableHttpResponse response1 = httpclient.execute(httpget);
HttpEntity entity3 = response1.getEntity();
System.out.println("----------------------------------------------get_respone1_ContentType="+entity3.getContentType());
System.out.println("----------------------------------------------get_respone1_ContentLength="+entity3.getContentLength());
System.out.println("VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVget_respone1_String="+EntityUtils.toString(entity3));
没能得到正确解析时的控制台log内容如下:
</pre></p><p></p><p>没能得到正确解析时的控制台log内容如下:</p><p><pre class="plain" name="code" snippet_file_name="blog_20160926_2_3861588" code_snippet_id="1902093">doView 里面 pointName检测点名称:null
timestampString=1474881335410
sign=a1fd47d6afb404251c1b5bea2ceeedc9
T_requesterString={"id":247502}
T_serviceDeskString={"id":1}
T_engineerString={"id":1}
17:15:35.417 [http-bio-8080-exec-90] DEBUG o.a.h.c.protocol.RequestAddCookies - CookieSpec selected: best-match
17:15:35.417 [http-bio-8080-exec-90] DEBUG o.a.h.c.protocol.RequestAuthCache - Auth cache not set in the context
17:15:35.417 [http-bio-8080-exec-90] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection request: [route: {}->http://utilitytunnel.ewei.com:80][total kept alive: 0; route allocated: 0 of 2; total allocated: 0 of 20]
17:15:35.417 [http-bio-8080-exec-90] DEBUG o.a.h.i.c.PoolingHttpClientConnectionManager - Connection leased: [id: 1][route: {}->http://utilitytunnel.ewei.com:80][total kept alive: 0; route allocated: 1 of 2; total allocated: 1 of 20]
17:15:35.417 [http-bio-8080-exec-90] DEBUG o.a.h.impl.execchain.MainClientExec - Opening connection {}->http://utilitytunnel.ewei.com:80
17:15:35.422 [http-bio-8080-exec-90] DEBUG o.a.h.i.c.HttpClientConnectionOperator - Connecting to utilitytunnel.ewei.com/121.41.76.156:80
17:15:35.532 [http-bio-8080-exec-90] DEBUG o.a.h.i.c.HttpClientConnectionOperator - Connection established 192.168.43.216:62400<->121.41.76.156:80
17:15:35.533 [http-bio-8080-exec-90] DEBUG o.a.h.impl.execchain.MainClientExec - Executing request GET /api/v1/customers.json HTTP/1.1
17:15:35.533 [http-bio-8080-exec-90] DEBUG o.a.h.impl.execchain.MainClientExec - Target auth state: UNCHALLENGED
17:15:35.533 [http-bio-8080-exec-90] DEBUG o.a.h.impl.execchain.MainClientExec - Proxy auth state: UNCHALLENGED
17:15:35.533 [http-bio-8080-exec-90] DEBUG org.apache.http.headers - http-outgoing-1 >> G