HttpClient is a HTTP/1.1 compliant HTTP agent implementation based on HttpCore. It also provides reusable components for client-side authentication, HTTP state management, and HTTP connection management. HttpComponents Client is a successor of and replacement for Commons HttpClient 3.x. Users of Commons HttpClient are strongly encouraged to upgrade.
总结: HttpClient 是apache 组织下面的一个用于处理HTTP 请求和响应的开源工具。
需要的jar包:
? httpclient-4.3.6.jar、httpcore-4.3.3.jar、httpmime-4.3.6.jar、commons-codec-1.6.jar。
由于HttpClient从4.X开始,基本的get/post 等http请求实现方式与之前版本的写法有很大的差异,所以在此总结一下4.X版本之后的官方推荐写法。
操作顺序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | package com.learn.http; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpEntity; import org.apache.http.NameValuePair; import org.apache.http.ParseException; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; public class { public static void main(String[] args) { new HttpSend().post( "http://www.baidu.com"); new HttpSend().get( "http://www.baidu.com"); } /** * 发送 post请求访问本地应用并根据传递参数不同返回不同结果 */ public void post(String url) { // 创建默认的httpClient实例. CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建httppost HttpPost httppost = new HttpPost(url); // 创建参数队列 List<NameValuePair> formparams = new ArrayList<NameValuePair>(); // 添加参数 // formparams.add(new BasicNameValuePair("gg", "1")); UrlEncodedFormEntity uefEntity; try { //传入空参数,创建实体对象 uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); //设置post对象的实体对象 httppost.setEntity(uefEntity); System.out.println( "executing post request " + httppost.getURI()); //执行post请求 CloseableHttpResponse response = httpclient.execute(httppost); try { //获取请求结果对象 HttpEntity entity = response.getEntity(); if (entity != null) { System.out.println( "--------------------------------------"); System.out.println( "Response content: " + EntityUtils.toString(entity)); System.out.println( "--------------------------------------"); } } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e1) { e1.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 发送 get请求 */ public void get(String url) { // 创建默认的httpClient实例. CloseableHttpClient httpclient = HttpClients.createDefault(); try { // 封装请求参数 List<NameValuePair> params = new ArrayList<>(); params.add( new BasicNameValuePair( "cityEname", "henan")); //转换为键值对 String str = EntityUtils.toString( new UrlEncodedFormEntity(params, "UTF-8")); System.out.println(str); //创建带参数get请求(示例) HttpGet httpGet = new HttpGet(url+ "?"+str); // 创建不带参数get请求 HttpGet httpget = new HttpGet(url); System.out.println( "executing get request " + httpget.getURI()); // 执行get请求. CloseableHttpResponse response = httpclient.execute(httpget); try { // 获取响应实体 HttpEntity entity = response.getEntity(); System.out.println( "--------------------------------------"); // 打印响应状态 System.out.println(response.getStatusLine()); if (entity != null) { // 打印响应内容长度 System.out.println( "Response content length: " + entity.getContentLength()); // 打印响应内容 System.out.println( "Response content: " + EntityUtils.toString(entity)); } System.out.println( "------------------------------------"); } finally { response.close(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭连接,释放资源 try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } } } |