HttpClient整理

1.  jar:httpclient-4.5.6.jar,httpcore.4.4.10.jar

2.代码案例:json格式发送数据

(1)HttpHeader:组装成header字段

  1 /**
  2  * 
  3  */
  4 package kklazy.test.entity;
  5 
  6 /**
  7  * @author whh
  8  *
  9  */
 10 /**
 11  * @author whh
 12  *
 13  */
 14 public class HttpHeader {
 15     private String VER;
 16     private String SN;
 17     private String REQ_FROM;
 18     private String TCI;
 19     private String PROC_DT;
 20     private String RESV1;
 21     /**
 22      * @return the vER
 23      */
 24     public String getVER() {
 25         return VER;
 26     }
 27     /**
 28      * @param vER the vER to set
 29      */
 30     public void setVER(String vER) {
 31         VER = vER;
 32     }
 33     /**
 34      * @return the sN
 35      */
 36     public String getSN() {
 37         return SN;
 38     }
 39     /**
 40      * @param sN the sN to set
 41      */
 42     public void setSN(String sN) {
 43         SN = sN;
 44     }
 45     /**
 46      * @return the rEQ_FROM
 47      */
 48     public String getREQ_FROM() {
 49         return REQ_FROM;
 50     }
 51     /**
 52      * @param rEQ_FROM the rEQ_FROM to set
 53      */
 54     public void setREQ_FROM(String rEQ_FROM) {
 55         REQ_FROM = rEQ_FROM;
 56     }
 57     /**
 58      * @return the tCI
 59      */
 60     public String getTCI() {
 61         return TCI;
 62     }
 63     /**
 64      * @param tCI the tCI to set
 65      */
 66     public void setTCI(String tCI) {
 67         TCI = tCI;
 68     }
 69     /**
 70      * @return the pROC_DT
 71      */
 72     public String getPROC_DT() {
 73         return PROC_DT;
 74     }
 75     /**
 76      * @param pROC_DT the pROC_DT to set
 77      */
 78     public void setPROC_DT(String pROC_DT) {
 79         PROC_DT = pROC_DT;
 80     }
 81     /**
 82      * @return the rESV1
 83      */
 84     public String getRESV1() {
 85         return RESV1;
 86     }
 87     /**
 88      * @param rESV1 the rESV1 to set
 89      */
 90     public void setRESV1(String rESV1) {
 91         RESV1 = rESV1;
 92     }
 93     /* (non-Javadoc)
 94      * @see java.lang.Object#toString()
 95      */
 96     @Override
 97     public String toString() {
 98         return "HttpHeader [VER=" + VER + ", SN=" + SN + ", REQ_FROM=" + REQ_FROM + ", TCI=" + TCI + ", PROC_DT="
 99                 + PROC_DT + ", RESV1=" + RESV1 + "]";
100     }
101     public HttpHeader(String vER, String sN, String rEQ_FROM, String tCI, String pROC_DT, String rESV1) {
102         super();
103         VER = vER;
104         SN = sN;
105         REQ_FROM = rEQ_FROM;
106         TCI = tCI;
107         PROC_DT = pROC_DT;
108         RESV1 = rESV1;
109     }
110     
111     
112     
113     
114     
115     
116 
117 }

(2)HttpBody:组装成body字段

  1 /**
  2  * 
  3  */
  4 package kklazy.test.entity;
  5 
  6 
  7 /**
  8  * @author whh
  9  *
 10  */
 11 public class HttpBody {
 12     private String FORM_ID;
 13     private String ORG_NO;
 14     private String MERCH_NO;
 15     private String TERM_NO;
 16     private String ACCT_NO;
 17     private String TRANS_AMT;
 18     private String TRANS_TYPE;
 19     /**
 20      * @return the fORM_ID
 21      */
 22     public String getFORM_ID() {
 23         return FORM_ID;
 24     }
 25     /**
 26      * @param fORM_ID the fORM_ID to set
 27      */
 28     public void setFORM_ID(String fORM_ID) {
 29         FORM_ID = fORM_ID;
 30     }
 31     /**
 32      * @return the oRG_NO
 33      */
 34     public String getORG_NO() {
 35         return ORG_NO;
 36     }
 37     /**
 38      * @param oRG_NO the oRG_NO to set
 39      */
 40     public void setORG_NO(String oRG_NO) {
 41         ORG_NO = oRG_NO;
 42     }
 43     /**
 44      * @return the mERCH_NO
 45      */
 46     public String getMERCH_NO() {
 47         return MERCH_NO;
 48     }
 49     /**
 50      * @param mERCH_NO the mERCH_NO to set
 51      */
 52     public void setMERCH_NO(String mERCH_NO) {
 53         MERCH_NO = mERCH_NO;
 54     }
 55     /**
 56      * @return the tERM_NO
 57      */
 58     public String getTERM_NO() {
 59         return TERM_NO;
 60     }
 61     /**
 62      * @param tERM_NO the tERM_NO to set
 63      */
 64     public void setTERM_NO(String tERM_NO) {
 65         TERM_NO = tERM_NO;
 66     }
 67     /**
 68      * @return the aCCT_NO
 69      */
 70     public String getACCT_NO() {
 71         return ACCT_NO;
 72     }
 73     /**
 74      * @param aCCT_NO the aCCT_NO to set
 75      */
 76     public void setACCT_NO(String aCCT_NO) {
 77         ACCT_NO = aCCT_NO;
 78     }
 79     /**
 80      * @return the tRANS_AMT
 81      */
 82     public String getTRANS_AMT() {
 83         return TRANS_AMT;
 84     }
 85     /**
 86      * @param tRANS_AMT the tRANS_AMT to set
 87      */
 88     public void setTRANS_AMT(String tRANS_AMT) {
 89         TRANS_AMT = tRANS_AMT;
 90     }
 91     
 92     /**
 93      * @return the tRANS_TYPE
 94      */
 95     public String getTRANS_TYPE() {
 96         return TRANS_TYPE;
 97     }
 98     /**
 99      * @param tRANS_TYPE the tRANS_TYPE to set
100      */
101     public void setTRANS_TYPE(String tRANS_TYPE) {
102         TRANS_TYPE = tRANS_TYPE;
103     }
104     
105     /* (non-Javadoc)
106      * @see java.lang.Object#toString()
107      */
108     @Override
109     public String toString() {
110         return "HttpBody [FORM_ID=" + FORM_ID + ", ORG_NO=" + ORG_NO + ", MERCH_NO=" + MERCH_NO + ", TERM_NO=" + TERM_NO
111                 + ", ACCT_NO=" + ACCT_NO + ", TRANS_AMT=" + TRANS_AMT + ", TRANS_TYPE=" + TRANS_TYPE + "]";
112     }
113     public HttpBody(String fORM_ID, String oRG_NO, String mERCH_NO, String tERM_NO, String aCCT_NO, String tRANS_AMT,
114             String tRANS_TYPE) {
115         super();
116         FORM_ID = fORM_ID;
117         ORG_NO = oRG_NO;
118         MERCH_NO = mERCH_NO;
119         TERM_NO = tERM_NO;
120         ACCT_NO = aCCT_NO;
121         TRANS_AMT = tRANS_AMT;
122         TRANS_TYPE = tRANS_TYPE;
123     }
124 
125     
126 }

(2)post/get方式发送请求:

  1 package kklazy.test;
  2 
  3 
  4 import java.io.IOException;
  5 import java.util.HashMap;
  6 import java.util.Map;
  7 
  8 import org.apache.http.HttpEntity;
  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.entity.StringEntity;
 13 import org.apache.http.impl.client.CloseableHttpClient;
 14 import org.apache.http.impl.client.HttpClients;
 15 import org.apache.http.util.EntityUtils;
 16 import org.junit.Test;
 17 import org.junit.runner.RunWith;
 18 import org.springframework.test.context.ContextConfiguration;
 19 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 20 
 21 import com.alibaba.fastjson.JSON;
 22 
 23 import kklazy.test.entity.HttpBody;
 24 import kklazy.test.entity.HttpHeader;
 25 
 26 @RunWith(SpringJUnit4ClassRunner.class)
 27 @ContextConfiguration(locations = { "classpath*:/beans/**/*.xml" })
 28 public class HttpJunitTest {
 29     private String url = "http://192.168.17.150:8080/yst/whiteNameListRiskControl/match";
 30     /**
 31      * post请求方式调用接口:不带参
 32      */
 33     @Test
 34     public  void doPost(){
 35         //创建一个httpclient对象
 36         CloseableHttpClient httpClient = null;
 37         CloseableHttpResponse response = null;
 38         //post对象
 39         HttpPost HttpPost = new HttpPost(url);
 40         try {
 41             httpClient = HttpClients.createDefault();
 42             //执行post请求
 43             response = httpClient.execute(HttpPost);
 44             int statusCode = response.getStatusLine().getStatusCode();
 45              if (statusCode == 200) {     
 46                  System.out.println("executing result---连接正常");
 47                 //获取响应结果
 48                    HttpEntity httpEntity = response.getEntity();
 49                 String responseString =  EntityUtils.toString(httpEntity);
 50                 // 获得当前方法名
 51                    String method = Thread.currentThread().getStackTrace()[1].getMethodName();
 52                    System.out.println(method+"响应结果:"+responseString);                 
 53                                
 54                } else {                
 55                    System.out.println("executing result---服务器连接异常");            
 56                }
 57         } catch (IOException e) {
 58             e.printStackTrace();
 59         }finally{
 60             if(null != response){
 61                 try {
 62                     response.close();
 63                 } catch (IOException e) {
 64                     e.printStackTrace();
 65                 }
 66             }
 67             if(null != httpClient){
 68                 try {
 69                     httpClient.close();
 70                 } catch (IOException e) {
 71                     e.printStackTrace();
 72                 }
 73             }
 74         }
 75     }
 76     /**
 77      * post请求方式调用接口:带参
 78      */
 79     @Test
 80     public void doPostWithParam(){
 81         /**组装设置请求参数*/
 82         HttpHeader HttpHeader = new HttpHeader("1.0", "31231", "MER_FRONT", "WECHAT_H5", "20180717153555", null);
 83         HttpBody HttpBody = new HttpBody("000000000008643", "61000016", "939310058120001", null, "01", "0000000011000", null);
 84         Map<String,Object> map = new HashMap<>();
 85         map.put("HEADER", HttpHeader);
 86         map.put("BODY",HttpBody );
 87         //map->json
 88         String  jsonMsg = JSON.toJSONString(map, true);//fastjson.jar -- com.alibaba.fastjson.JSON
 89         // 获得当前方法名
 90         String method = Thread.currentThread().getStackTrace()[1].getMethodName();
 91         System.out.println(method+"请求的json数据为:"+jsonMsg);
 92         StringEntity stringEntity = new StringEntity(jsonMsg,"UTF-8");
 93         stringEntity.setContentEncoding("UTF-8");
 94         stringEntity.setContentType("text/json");
 95         //创建一个httpclient对象
 96         CloseableHttpClient httpClient = null;
 97         CloseableHttpResponse  response=null;//响应结果
 98         //创建一个Post对象
 99         HttpPost httpPost  = new HttpPost(url);
100         httpPost.setEntity(stringEntity);
101         //执行请求,获取响应
102         try {
103             httpClient = HttpClients.createDefault();
104             response= httpClient.execute(httpPost);
105             int statusCode = response.getStatusLine().getStatusCode();
106              if (statusCode == 200) {   
107                 System.out.println("executing result---连接正常");
108                 HttpEntity httpEntity = response.getEntity();
109                 String responseString =  EntityUtils.toString(httpEntity);
110                 System.out.println(method+"响应结果:"+responseString);                 
111             } else {                
112                 System.out.println("executing result---服务器连接异常");            
113             }
114         } catch (Exception e) {
115             e.printStackTrace();
116         } finally{
117             if(null != response){
118                 try {
119                     response.close();
120                 } catch (IOException e) {
121                     e.printStackTrace();
122                 }
123             }
124             if(null != httpClient){
125                 try {
126                     httpClient.close();
127                 } catch (IOException e) {
128                     e.printStackTrace();
129                 }
130             }
131         }
132     }
133     /**
134      * get请求方式调用接口:不带参
135      */
136     @Test
137     public void doGet(){
138         CloseableHttpClient httpClient = null;
139         CloseableHttpResponse  response=null;
140         HttpGet HttpGet = new HttpGet(url);
141         try {
142             httpClient = HttpClients.createDefault();
143             response= httpClient.execute(HttpGet);
144             int statusCode = response.getStatusLine().getStatusCode();
145             //获取当前方法名
146             String method = Thread.currentThread().getStackTrace()[1].getMethodName();
147             System.out.println(method+"statusCode:"+statusCode);
148             if (statusCode == 200) {
149                 System.out.println("executing result---连接正常");
150                 HttpEntity httpEntity = response.getEntity();
151                 String responseString =  EntityUtils.toString(httpEntity);
152                 System.out.println(method+"响应结果:"+responseString);                 
153             } else {                
154                 System.out.println("executing result---服务器连接异常");            
155             }
156         } catch (IOException e) {
157             e.printStackTrace();
158             System.out.println("Exception================" + e.toString());
159         } finally{
160             if(null != response){
161                 try {
162                     response.close();
163                 } catch (IOException e) {
164                     e.printStackTrace();
165                 }
166             }
167             if(null != httpClient){
168                 try {
169                     httpClient.close();
170                 } catch (IOException e) {
171                     e.printStackTrace();
172                 }
173             }
174         }
175         
176     }
177     
178     
179     
180 }
原文地址:https://www.cnblogs.com/whhjava/p/9804834.html