???????????????У?????鳤???????????????????????£??????httpЭ?飬????????????????httpClient???????????????????飬??????????????????????????demo?????????????????????????????????????????demo?????????????????????????????????????????????????в????????????????????????????????
?????????jar????

1packagecom.lym.test;
2
3importorg.apache.http.HttpEntity;
4importorg.apache.http.HttpHost;
5importorg.apache.http.client.config.RequestConfig;
6importorg.apache.http.client.methods.CloseableHttpResponse;
7importorg.apache.http.client.methods.HttpPost;
8importorg.apache.http.entity.StringEntity;
9importorg.apache.http.impl.client.CloseableHttpClient;
10importorg.apache.http.impl.client.HttpClientBuilder;
11importorg.apache.http.util.EntityUtils;
12
13importcom.google.gson.JsonObject;
14
15publicclassHttpClientTest{
16
17publicstaticvoidmain(Stringargs[])throwsException{
18
19//????HttpClientBuilder
20HttpClientBuilderhttpClientBuilder=HttpClientBuilder.create();
21//HttpClient
22CloseableHttpClientcloseableHttpClient=httpClientBuilder.build();
23//??????????????????????Э??????
24HttpHosttarget=newHttpHost("61.144.244.6:8888/sztmerchant/merchant/addIsztMerchant.htm"??8888??"http");
25//?????????????????????Э??????
26HttpHostproxy=newHttpHost("proxy3.bj.petrochina"??8080??"http");
27RequestConfigconfig=RequestConfig.custom().setProxy(proxy).build();
28
29//??????
30HttpPosthttpPost=newHttpPost("http://61.144.244.6:8888/sztmerchant/merchant/addIsztMerchant.htm");
31//????????
32httpPost.addHeader("Content-type"??"application/json;charset=utf-8");
33httpPost.setHeader("Accept"??"application/json");
34httpPost.setConfig(config);
35
36//????????json??
37JsonObjectjsonObj=newJsonObject();
38jsonObj.addProperty("merchantNo"??"33300911238");
39jsonObj.addProperty("merchantName"??"???????????????1238");
40StringjsonStr=jsonObj.toString();
41System.out.println("parameters:"+jsonStr);
42
43StringEntityentity;
44try{
45entity=newStringEntity(jsonStr??"UTF-8");
46httpPost.setEntity(entity);
47CloseableHttpResponseresponse=closeableHttpClient.execute(target??httpPost);
48//getEntity()
49HttpEntityhttpEntity=response.getEntity();
50if(httpEntity!=null){
51//??????????
52System.out.println("result:"+EntityUtils.toString(httpEntity??"UTF-8"));
53}else{
54System.out.println("?????????");
55}
56//??????
57if(closeableHttpClient!=null){
58closeableHttpClient.close();
59}
60}catch(Exceptione){
61e.printStackTrace();
62}
63}
64
65}