HttpClient是Apache Jakarta Common下的子项目,用来提供高效的、最新的、功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。HttpClient已经应用在很多的项目中。
下载地址:https://hc.apache.org/httpclient-legacy/
不过一般在项目中是直接调用pom依赖
<dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.13</version> </dependency>
使用HttpClient发送请求和接收响应一般有以下步骤:
- 创建HttpClient对象。
- 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
- 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HttpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
- 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
- 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
- 释放连接。无论执行方法是否成功,都必须释放连接
public void httpClientTest() throws Exception{ CloseableHttpClient httpClient= HttpClients.createDefault(); //不带参数 //HttpGet get=new HttpGet("http://www.it.cn"); //带参数 HttpGet get=new HttpGet("http://yun.com/course/936.html?capid=1$czpc$jingjiaczpz-PC-1"); CloseableHttpResponse response=httpClient.execute(get); StatusLine statusLine=response.getStatusLine(); System.out.println(statusLine); int statusCode=statusLine.getStatusCode(); System.out.println(statusCode); HttpEntity entity= response.getEntity(); String html= EntityUtils.toString(entity,"utf-8"); System.out.println(html); response.close(); httpClient.close(); }
public void testPost() throws Exception{ CloseableHttpClient httpClient=HttpClients.createDefault(); HttpPost post=new HttpPost("http://yun.com/course/936.html"); //创建list存放请求表单 List<NameValuePair>form=new ArrayList<>(); //较多的提交数据可以增元素 form.add(new BasicNameValuePair("capid","1$czpc$jingjiaczpz-PC-1")); // form.add(new BasicNameValuePair("capid","1$czpc$jingjiaczpz-PC-1")); //form.add(new BasicNameValuePair("capid","1$czpc$jingjiaczpz-PC-1")); HttpEntity entity=new UrlEncodedFormEntity(form); post.setEntity(entity); CloseableHttpResponse response= httpClient.execute(post); String s = EntityUtils.toString(response.getEntity(), "utf-8"); System.out.println(s); response.close(); httpClient.close(); }
public void testPoolingConnectManager() throws Exception{ PoolingHttpClientConnectionManager cm=new PoolingHttpClientConnectionManager(); CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build(); HttpGet get=new HttpGet("http://www.it.cn"); CloseableHttpResponse execute = httpClient.execute(get); String s = EntityUtils.toString(execute.getEntity(), "utf-8"); System.out.println(s); execute.close(); //此处不需要关闭连接池 会直接被连接池回收 }