java經過HttpClient調用接口總結

2.HttpClientapache

  2.1簡介:cookie

       

最近看項目的代碼,看到工程中有兩個jar包張的很像,一個是commons.httpclient-3.1.jar,一個是httpclient4.2.1.jar,很納悶,並且這兩個包裏都有HttpClient這個類,可是包名卻不同,而後就查找了一番資料,看下這兩個包究竟是個什麼關係。app


commons.httpclient-3.1.jar這個jar包是依賴了httpcore,commons-logging,commons-codec這三個jar包。而在commons-httpclient.jar包在使用過程當中,須要依賴commons-logging.jar和commons-codec.jar,這個httpclient4.2.1.jar就至關於集合了之前的commons-httpclient,commons-logging,commons-codec,這三個jar包,減小了繁瑣的導入jar包的過程,如今只需導入一個httpclient.jar就能夠使用了。code

除此之外,在apache的官網上下載httpclient的jar包時,會發現它是在一個叫HttpComponent的項目下,這個HttpComponent是apache的頂級項目。而之前的commons的那三個包都是commons的項目下。在HttpComponent的網頁上,看到了這樣的文字: HttpComponents Client is a successor of and replacement for Commons HttpClient 3.x. Users of Commons HttpClient are strongly encouraged to upgrade.get

也就是說commons下的commons-httpclient不在更新和維護了,用commons-httpclient的用戶也建議更新到httpclient這個包。it

 

HttpClient httpClient = new HttpClient();
httpClient.getHostConfiguration().setProxy("10.91.237.89", 3128);
GetMethod getMethod = new GetMethod(saleUserInfoURL + saleToken);
getMethod.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
getMethod .addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36");
int httpStatus = httpClient.executeMethod(getMethod);
if (HttpStatus.SC_OK == httpStatus) {
byte[] result = getMethod.getResponseBody();
buffer.append(new String(result));
if (StringUtils.isNotEmpty(buffer.toString())) {
Map<String, Object> result2 = JSONObject.parseObject(buffer.toString(), Map.class);
return result2;
}
}io

  

 

  2.2 GET方式調用class

 

  2.3 POST方式調用cli

相關文章
相關標籤/搜索