closeableHttpClient和HttpClient不同
声明:本文转载自新浪微博,非本人所写
HttpClient 4.3与4.3版本以下版本比较 -
【加入收藏夹】 【打印】 【关闭】 来源: 日期:2015-07-22 22:00:57 点击量: 282 收藏
网上利用java发送http请求的代码很多,一搜一大把,有的利用的是java.net.*下的HttpURLConnection,有的用httpclient,而且发送的代码也分门别类。今天我们主要来说的是利用httpclient发送请求。
httpclient又可分为
- httpclient3.x
- httpclient4.x到httpclient4.3以下
- httpclient4.3以上
不同httpclient版本其请求发送的方式也不一样,下面来做个归纳
httpclient3.x
HttpClient client = new HttpClient(); // 设置代理服务器地址和端口 // client.getHostConfiguration().setProxy("proxy_host_addr",proxy_port); // 使用 GET 方法 ,如果服务器需要通过 HTTPS 连接,那只需要将下面 URL 中的 http 换成 https HttpMethodmethod = new GetMethod("http://java.sun.com"); // 使用POST方法 // HttpMethod method = new PostMethod("http://java.sun.com"); client.executeMethod(method); // 打印服务器返回的状态 System.out.println(method.getStatusLine()); // 打印返回的信息 System.out.println(method.getResponseBodyAsString()); // 释放连接 method.releaseConnection();
httpclient4.x到httpclient4.3以下
public void getUrl(String url, String encoding) throws ClientProtocolException, IOException { HttpClient client = new DefaultHttpClient(); HttpGet get = new HttpGet(url); HttpResponse response = client.execute(get); HttpEntity entity = response.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(instream, encoding)); System.out.println(reader.readLine()); } catch (Exception e) { e.printStackTrace(); } finally { instream.close(); } } // 关闭连接. client.getConnectionManager().shutdown(); }
httpclient4.3以上
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; public static String getResult(String urlStr) { CloseableHttpClient httpClient = HttpClients.createDefault(); // HTTP Get请求 HttpGet httpGet = new HttpGet(urlStr); // 设置请求和传输超时时间 // RequestConfig requestConfig = // RequestConfig.custom().setSocketTimeout(TIME_OUT).setConnectTimeout(TIME_OUT).build(); // httpGet.setConfig(requestConfig); String res = ""; try { // 执行请求 HttpResponse getAddrResp = httpClient.execute(httpGet); HttpEntity entity = getAddrResp.getEntity(); if (entity != null) { res = EntityUtils.toString(entity); } log.info("响应" + getAddrResp.getStatusLine()); } catch (Exception e) { log.error(e.getMessage(), e); return res; } finally { try { httpClient.close(); } catch (IOException e) { log.error(e.getMessage(), e); return res; } } return res; }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义