HttpClient 4.3与4.3版本以下版本 close 比较

网上利用java发送http请求的代码很多,一搜一大把,有的利用的是java.net.*下的HttpURLConnection,有的用httpclient,而且发送的代码也分门别类。今天我们主要来说的是利用httpclient发送请求。

httpclient又可分为

  • httpclient3.x
  • httpclient4.x到httpclient4.3以下
  • httpclient4.3以上

不同httpclient版本其请求发送的方式也不一样,下面来做个归纳

 

httpclient3.x

Java代码   收藏代码
  1. HttpClient client = new HttpClient();  
  2. // 设置代理服务器地址和端口  
  3. // client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port);  
  4. // 使用 GET 方法 ,如果服务器需要通过 HTTPS 连接,那只需要将下面 URL 中的 http 换成 https  
  5. HttpMethodmethod = new GetMethod("http://java.sun.com");  
  6. // 使用POST方法  
  7. // HttpMethod method = new PostMethod("http://java.sun.com");  
  8. client.executeMethod(method);  
  9. // 打印服务器返回的状态  
  10. System.out.println(method.getStatusLine());  
  11. // 打印返回的信息  
  12. System.out.println(method.getResponseBodyAsString());  
  13. // 释放连接  
  14. method.releaseConnection();  

 

 

httpclient4.x到httpclient4.3以下

 

Java代码   收藏代码
  1. public void getUrl(String url, String encoding) throws ClientProtocolException, IOException {  
  2.         HttpClient client = new DefaultHttpClient();  
  3.         HttpGet get = new HttpGet(url);  
  4.                 //设置超时时间  
  5.                 HttpParams params = client.getParams();  
  6.                 HttpConnectionParams.setConnectionTimeout(params, (int10 * 1000);  
  7.                 HttpConnectionParams.setSoTimeout(params, 10 * 1000);  
  8.         HttpResponse response = client.execute(get);  
  9.         HttpEntity entity = response.getEntity();  
  10.         if (entity != null) {  
  11.             InputStream instream = entity.getContent();  
  12.             try {  
  13.                 BufferedReader reader = new BufferedReader(new InputStreamReader(instream, encoding));  
  14.                 System.out.println(reader.readLine());  
  15.             } catch (Exception e) {  
  16.                 e.printStackTrace();  
  17.             } finally {  
  18.                 instream.close();  
  19.             }  
  20.         }  
  21.         // 关闭连接.  
  22.         client.getConnectionManager().shutdown();  
  23.     }  

 

 

 

httpclient4.3以上

 

Java代码   收藏代码
  1. import org.apache.http.HttpEntity;  
  2. import org.apache.http.HttpResponse;  
  3. import org.apache.http.client.methods.HttpGet;  
  4. import org.apache.http.impl.client.CloseableHttpClient;  
  5. import org.apache.http.impl.client.HttpClients;  
  6. import org.apache.http.util.EntityUtils;  
  7.   
  8.   
  9. public static String getResult(String urlStr) {  
  10.         CloseableHttpClient httpClient = HttpClients.createDefault();  
  11.         // HTTP Get请求  
  12.         HttpGet httpGet = new HttpGet(urlStr);  
  13.         // 设置请求和传输超时时间  
  14.         // RequestConfig requestConfig =  
  15.         // RequestConfig.custom().setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).build();  
  16.         // httpGet.setConfig(requestConfig);  
  17.         String res = "";  
  18.         try {  
  19.             // 执行请求  
  20.             HttpResponse getAddrResp = httpClient.execute(httpGet);  
  21.             HttpEntity entity = getAddrResp.getEntity();  
  22.             if (entity != null) {  
  23.                 res = EntityUtils.toString(entity);  
  24.             }  
  25.             log.info("响应" + getAddrResp.getStatusLine());  
  26.         } catch (Exception e) {  
  27.             log.error(e.getMessage(), e);  
  28.             return res;  
  29.         } finally {  
  30.             try {  
  31.                 httpClient.close();  
  32.             } catch (IOException e) {  
  33.                 log.error(e.getMessage(), e);  
  34.                 return res;  
  35.             }  
  36.         }  
  37.         return res;  
  38.     }  

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值