转载自:
一、使用方法
使用HttpClient发送请求、接收响应很简单,一般需要如下几步即可。
1. 创建HttpClient对象。
2. 创建请求方法的实例,并指定请求URL。如果需要发送GET请求,创建HttpGet对象;如果需要发送POST请求,创建HttpPost对象。
3. 如果需要发送请求参数,可调用HttpGet、HttpPost共同的setParams(HetpParams params)方法来添加请求参数;对于HttpPost对象而言,也可调用setEntity(HttpEntity entity)方法来设置请求参数。
4. 调用HttpClient对象的execute(HttpUriRequest request)发送请求,该方法返回一个HttpResponse。
5. 调用HttpResponse的getAllHeaders()、getHeaders(String name)等方法可获取服务器的响应头;调用HttpResponse的getEntity()方法可获取HttpEntity对象,该对象包装了服务器的响应内容。程序可通过该对象获取服务器的响应内容。
6. 释放连接。无论执行方法是否成功,都必须释放连接
二、实例
1 import java.io.IOException; 2 import java.net.URI; 3 import java.util.ArrayList; 4 import java.util.List; 5 import java.util.Map; 6 7 import org.apache.http.NameValuePair; 8 import org.apache.http.client.entity.UrlEncodedFormEntity; 9 import org.apache.http.client.methods.CloseableHttpResponse; 10 import org.apache.http.client.methods.HttpGet; 11 import org.apache.http.client.methods.HttpPost; 12 import org.apache.http.client.utils.URIBuilder; 13 import org.apache.http.entity.ContentType; 14 import org.apache.http.entity.StringEntity; 15 import org.apache.http.impl.client.CloseableHttpClient; 16 import org.apache.http.impl.client.HttpClients; 17 import org.apache.http.message.BasicNameValuePair; 18 import org.apache.http.util.EntityUtils; 19 20 21 public static String doGet(String url, Mapparam) { 22 23 // 创建Httpclient对象 24 CloseableHttpClient httpclient = HttpClients.createDefault(); 25 26 String resultString = ""; 27 CloseableHttpResponse response = null; 28 try { 29 // 创建uri 30 URIBuilder builder = new URIBuilder(url); 31 if (param != null) { 32 for (String key : param.keySet()) { 33 builder.addParameter(key, param.get(key)); 34 } 35 } 36 URI uri = builder.build(); 37 38 // 创建http GET请求 39 HttpGet httpGet = new HttpGet(uri); 40 41 // 执行请求 42 response = httpclient.execute(httpGet); 43 // 判断返回状态是否为200 44 if (response.getStatusLine().getStatusCode() == 200) { 45 resultString = EntityUtils.toString(response.getEntity(), "UTF-8"); 46 } 47 } catch (Exception e) { 48 e.printStackTrace(); 49 } finally { 50 try { 51 if (response != null) { 52 response.close(); 53 } 54 httpclient.close(); 55 } catch (IOException e) { 56 e.printStackTrace(); 57 } 58 } 59 return resultString; 60 } 61 62 public static String doGet(String url) { 63 return doGet(url, null); 64 } 65 66 public static String doPost(String url, Map param) { 67 // 创建Httpclient对象 68 CloseableHttpClient httpClient = HttpClients.createDefault(); 69 CloseableHttpResponse response = null; 70 String resultString = ""; 71 try { 72 // 创建Http Post请求 73 HttpPost httpPost = new HttpPost(url); 74 // 创建参数列表 75 if (param != null) { 76 List paramList = new ArrayList<>(); 77 for (String key : param.keySet()) { 78 paramList.add(new BasicNameValuePair(key, param.get(key))); 79 } 80 // 模拟表单 81 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(paramList); 82 httpPost.setEntity(entity); 83 } 84 // 执行http请求 85 response = httpClient.execute(httpPost); 86 resultString = EntityUtils.toString(response.getEntity(), "utf-8"); 87 } catch (Exception e) { 88 e.printStackTrace(); 89 } finally { 90 try { 91 response.close(); 92 } catch (IOException e) { 93 // TODO Auto-generated catch block 94 e.printStackTrace(); 95 } 96 } 97 98 return resultString; 99 }100 101 public static String doPost(String url) {102 return doPost(url, null);103 }104 105 public static String doPostJson(String url, String json) {106 // 创建Httpclient对象107 CloseableHttpClient httpClient = HttpClients.createDefault();108 CloseableHttpResponse response = null;109 String resultString = "";110 try {111 // 创建Http Post请求112 HttpPost httpPost = new HttpPost(url);113 // 创建请求内容114 StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);115 httpPost.setEntity(entity);116 // 执行http请求117 response = httpClient.execute(httpPost);118 resultString = EntityUtils.toString(response.getEntity(), "utf-8");119 } catch (Exception e) {120 e.printStackTrace();121 } finally {122 try {123 response.close();124 } catch (IOException e) {125 // TODO Auto-generated catch block126 e.printStackTrace();127 }128 }129 130 return resultString;131 }
3、用到的一些jar包
https://pan.baidu.com/s/1kNZWQoZNyYEudL7DbotT0A k4yt