发送HTTP请求 -- HttpUtil

1.

  1 package com.step.utils;
  2 
  3 import java.io.IOException;
  4 import java.net.URLDecoder;
  5 import java.util.ArrayList;
  6 import java.util.List;
  7 
  8 import org.apache.http.HttpResponse;
  9 import org.apache.http.HttpStatus;
 10 import org.apache.http.NameValuePair;
 11 import org.apache.http.client.ClientProtocolException;
 12 import org.apache.http.client.HttpClient;
 13 import org.apache.http.client.entity.UrlEncodedFormEntity;
 14 import org.apache.http.client.methods.HttpGet;
 15 import org.apache.http.client.methods.HttpPost;
 16 import org.apache.http.entity.StringEntity;
 17 import org.apache.http.impl.client.DefaultHttpClient;
 18 import org.apache.http.message.BasicNameValuePair;
 19 import org.apache.http.util.EntityUtils;
 20 
 21 import com.alibaba.fastjson.JSONObject;
 22 
 23 
 24 
 25 /**
 26  * 发送HTTP请求
 27  * @author mlu
 28  *
 29  */
 30 public class HttpUtils {
 31     
 32     /**
 33      * 发送post请求--用于接口接收的参数为JSON字符串
 34      * @param url 请求地址
 35      * @param params json格式的字符串
 36      * @return
 37      */
 38     public static String httpPost(String url, String params){
 39         String result = null;
 40         try {
 41             HttpClient httpClient = new DefaultHttpClient();
 42             HttpPost httpPost = new HttpPost(url);
 43             /*
 44              * 发送json字符串,这两句需要设置
 45              */
 46             httpPost.addHeader("Content-type","application/json; charset=utf-8");  
 47             httpPost.setHeader("Accept", "application/json");  
 48             
 49             httpPost.setEntity(new StringEntity(params, "UTF-8"));
 50             
 51             HttpResponse response = httpClient.execute(httpPost);  
 52             
 53             int statusCode = response.getStatusLine().getStatusCode();  
 54               
 55             if (statusCode == HttpStatus.SC_OK) {  
 56                 // Read the response body  
 57                 result = EntityUtils.toString(response.getEntity(),"UTF-8");  
 58             } 
 59         } catch (Exception e) {
 60             e.printStackTrace();
 61         }
 62         return result;
 63     }
 64     
 65     /**
 66      * 发送post请求--用于接口接收的参数为键值对
 67      * @param url 请求地址
 68      * @param nameValuePairs 键值对
 69      * @return
 70      */
 71     public static String httpPost(String url, List<NameValuePair> nameValuePairs) {
 72         HttpClient httpClient = new DefaultHttpClient();
 73         HttpPost httpPost = new HttpPost(url);
 74         String strResult = "";
 75 
 76         try {
 77             httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
 78             HttpResponse response = httpClient.execute(httpPost);
 79 
 80             if (response.getStatusLine().getStatusCode() == 200) {
 81                 /* 读返回数据 */
 82                 strResult = EntityUtils.toString(response.getEntity());
 83                 // System.out.println("conResult:"+conResult);
 84             } else {
 85                 strResult += "发送失败:" + response.getStatusLine().getStatusCode();
 86             }
 87         } catch (ClientProtocolException e) {
 88             e.printStackTrace();
 89         } catch (IOException e) {
 90             e.printStackTrace();
 91         }
 92 
 93         return strResult;
 94     }
 95     
 96     public static String httpGet(String url, List<NameValuePair> nameValuePairs){
 97         HttpClient httpClient = new DefaultHttpClient();
 98         String sb = "";
 99         String result = "";
100         try {
101             for(NameValuePair nvp:nameValuePairs){
102                 sb += nvp.getName()+"="+nvp.getValue()+"&";
103             }
104             sb = sb.substring(0,sb.length()-1);
105             sb = URLDecoder.decode(sb, "UTF-8");
106             HttpGet httpGet = new HttpGet(url+"?"+sb);
107         
108             HttpResponse response = httpClient.execute(httpGet);
109             if (response.getStatusLine().getStatusCode() == 200) {
110                 /* 读返回数据 */
111                 result = EntityUtils.toString(response.getEntity());
112             } else {
113                 result += "发送失败:" + response.getStatusLine().getStatusCode();
114             }
115             
116         } catch (ClientProtocolException e) {
117             e.printStackTrace();
118         } catch (IOException e) {
119             e.printStackTrace();
120         }
121         return result;
122     }
123     
124     public static void main(String[] args) {
125         String url = "http://10.140.8.56/gd_fssc/rest/fsscService/getStaffInfo";
126         String url2 = "http://localhost:8080/eshore-app-backframe-web/interface/getJson";
127 
128         // 发送 POST 请求
129         JSONObject json = new JSONObject();
130         json.put("number", "44053211@GD");
131         httpPost(url,json.toString());
132         
133         List<NameValuePair> nameValuePairs = new ArrayList<>();
134         nameValuePairs.add(new BasicNameValuePair("method", "login"));
135         httpGet(url2,nameValuePairs);
136     }
137 }

引用的jar包:

原文地址:https://www.cnblogs.com/sharpest/p/7789208.html