Http请求1

  1 package Test;
  2 
  3 import java.io.IOException;
  4 import java.io.InputStreamReader;
  5 import java.net.URISyntaxException;
  6 import java.util.ArrayList;
  7 import java.util.List;
  8 import java.util.Map;
  9 import org.apache.http.NameValuePair;
 10 import org.apache.http.HttpEntity;
 11 import org.apache.http.client.ClientProtocolException;
 12 import org.apache.http.client.methods.CloseableHttpResponse;
 13 import org.apache.http.client.methods.HttpGet;
 14 import org.apache.http.client.methods.HttpPost;
 15 import org.apache.http.client.utils.URIBuilder;
 16 import org.apache.http.entity.ContentType;
 17 import org.apache.http.entity.StringEntity;
 18 import org.apache.http.impl.client.CloseableHttpClient;
 19 import org.apache.http.impl.client.HttpClients;
 20 import org.apache.http.message.BasicNameValuePair;
 21 import org.apache.http.util.CharArrayBuffer;
 22 import org.apache.http.util.EntityUtils;
 23 
 24 public class HttpClient {
 25     /**
 26      * 包含字符串参数的HttpPost请求
 27      */
 28     public static String doPostStr(String url, String string) throws IOException {
 29         String resp = null;
 30         StringEntity entityStr = new StringEntity(string,ContentType.create("text/plain", "UTF-8"));
 31         CloseableHttpClient httpClient = HttpClients.createDefault();
 32         HttpPost httpPost = new HttpPost(url);
 33         httpPost.setEntity(entityStr);
 34         CloseableHttpResponse response = null;
 35         try {
 36             response = httpClient.execute(httpPost);
 37             HttpEntity entity = response.getEntity();
 38             resp = EntityUtils.toString(entity, "UTF-8");
 39             EntityUtils.consume(entity);
 40         } catch (ClientProtocolException e) {
 41             e.printStackTrace();
 42         } catch (IOException e) {
 43             e.printStackTrace();
 44         } finally {
 45             if (response != null) {
 46                 try {
 47                     response.close();
 48                 } catch (IOException e) {
 49                     e.printStackTrace();
 50                 }
 51             }
 52         }
 53         return resp;
 54     }
 55     /**
 56      * 不含参数的HttpPost请求
 57      */
 58     public static String doPost(String url) throws IOException {
 59         CloseableHttpClient httpclient = HttpClients.createDefault();
 60         HttpPost httppost = new HttpPost(url);
 61         CloseableHttpResponse response = null;
 62         try {
 63             response = httpclient.execute(httppost);
 64         } catch (IOException e) {
 65             e.printStackTrace();
 66         }
 67         HttpEntity entity = response.getEntity();
 68         String result = null;
 69         result = EntityUtils.toString(entity);
 70         return result;
 71     }
 72     /**
 73      * 包含Map参数的HttpGet请求
 74      */
 75     public static String doGetMap(String url, Map<String,String> map) throws IOException {
 76         String result = null;
 77         CloseableHttpClient httpClient = HttpClients.createDefault();
 78         List<NameValuePair> pairs = new ArrayList<NameValuePair>();
 79         for(Map.Entry<String,String> entry : map.entrySet()){
 80             pairs.add(new BasicNameValuePair(entry.getKey(),entry.getValue()));
 81         }
 82         CloseableHttpResponse response = null;
 83         try {
 84             URIBuilder builder = new URIBuilder(url);
 85             builder.setParameters(pairs);
 86             HttpGet get = new HttpGet(builder.build());
 87             response = httpClient.execute(get);
 88             if(response != null && response.getStatusLine().getStatusCode() == 200){
 89                 HttpEntity entity = response.getEntity();
 90                 long lenth = entity.getContentLength();
 91                 if(lenth != -1 && lenth < 2048){
 92                     result = EntityUtils.toString(entity,"UTF-8");
 93                 }else {
 94                     InputStreamReader reader1 = new InputStreamReader(entity.getContent(), "UTF-8");
 95                     CharArrayBuffer buffer = new CharArrayBuffer(2048);
 96                     char[] tmp = new char[1024];
 97                     int l;
 98                     while((l = reader1.read(tmp)) != -1) {
 99                         buffer.append(tmp, 0, l);
100                     }
101                     result = buffer.toString();
102                 }
103             }
104         } catch (URISyntaxException e) {
105             e.printStackTrace();
106         } catch (ClientProtocolException e) {
107             e.printStackTrace();
108         } catch (IOException e) {
109             e.printStackTrace();
110         }finally {
111             try {
112                 httpClient.close();
113                 if(response != null)
114                 {
115                     response.close();
116                 }
117             } catch (IOException e) {
118                 e.printStackTrace();
119             }
120         }
121         return result;
122     }
123     /**
124      * 发送不带参数的HttpGet请求
125      */
126     public static String sendGet(String url) throws IOException {
127         CloseableHttpClient httpclient = HttpClients.createDefault();
128         HttpGet httpget = new HttpGet(url);
129         CloseableHttpResponse response = null;
130         try {
131             response = httpclient.execute(httpget);
132         } catch (IOException e) {
133             e.printStackTrace();
134         }
135         HttpEntity entity = response.getEntity();
136         String result = null;
137         result = EntityUtils.toString(entity);
138         return result;
139     }
140 }
原文地址:https://www.cnblogs.com/goatherd/p/10840131.html