一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - HttpClient實現調用外部項目接口工具類的示例

HttpClient實現調用外部項目接口工具類的示例

2021-01-14 16:16小曉峰 Java教程

下面小編就為大家帶來一篇HttpClient實現調用外部項目接口工具類的示例。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

實例如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import org.apache.http.NameValuePair;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.DefaultHostnameVerifier;
import org.apache.http.conn.util.PublicSuffixMatcher;
import org.apache.http.conn.util.PublicSuffixMatcherLoader;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
 
public class HttpUtils {
 private static RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(15000).setConnectTimeout(15000)
  .setConnectionRequestTimeout(15000).build();
 
 public static String sendHttpGet(HttpGet httpGet) {
 CloseableHttpClient httpClient = null;
 CloseableHttpResponse response = null;
 HttpEntity entity = null;
 String responseContent = null;
 try {
  // 創建默認的httpClient實例.
  httpClient = HttpClients.createDefault();
  httpGet.setConfig(requestConfig);
  
  // 執行請求
  response = httpClient.execute(httpGet);
  entity = response.getEntity();
  responseContent = EntityUtils.toString(entity, "UTF-8");
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  try {
  // 關閉連接,釋放資源
  if (response != null) {
   response.close();
  }
  if (httpClient != null) {
   httpClient.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 return responseContent;
 }
 /**
   * 發送 post請求
   * @param httpUrl 地址
   * @param maps 參數
   */
  public static String sendHttpPost(String httpUrl, Map<String, String> maps) {
    HttpPost httpPost = new HttpPost(httpUrl);// 創建httpPost 
    // 創建參數隊列 
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    for (String key : maps.keySet()) {
      nameValuePairs.add(new BasicNameValuePair(key, maps.get(key)));
    }
    try {
      httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
    } catch (Exception e) {
      e.printStackTrace();
    }
    return sendHttpPost(httpPost);
  }
   
   
 public static String sendHttpPost(HttpPost httpPost) {
 CloseableHttpClient httpClient = null;
 CloseableHttpResponse response = null;
 HttpEntity entity = null;
 String responseContent = null;
 try {
  // 創建默認的httpClient實例.
  httpClient = HttpClients.createDefault();
  httpPost.setConfig(requestConfig);
  // 執行請求
  response = httpClient.execute(httpPost);
  entity = response.getEntity();
  responseContent = EntityUtils.toString(entity, "UTF-8");
 } catch (Exception e) {
  e.printStackTrace();
 } finally {
  try {
  // 關閉連接,釋放資源
  if (response != null) {
   response.close();
  }
  if (httpClient != null) {
   httpClient.close();
  }
  } catch (IOException e) {
  e.printStackTrace();
  }
 }
 return responseContent;
 }
 
 /**
   * 發送Get請求Https
   * @param httpPost
   * @return
   */
  public static String sendHttpsGet(HttpGet httpGet) {
    CloseableHttpClient httpClient = null;
    CloseableHttpResponse response = null;
    HttpEntity entity = null;
    String responseContent = null;
    try {
      // 創建默認的httpClient實例.
      PublicSuffixMatcher publicSuffixMatcher = PublicSuffixMatcherLoader.load(new URL(httpGet.getURI().toString()));
      DefaultHostnameVerifier hostnameVerifier = new DefaultHostnameVerifier(publicSuffixMatcher);
      httpClient = HttpClients.custom().setSSLHostnameVerifier(hostnameVerifier).build();
      httpGet.setConfig(requestConfig);
      // 執行請求
      response = httpClient.execute(httpGet);
      entity = response.getEntity();
      responseContent = EntityUtils.toString(entity, "UTF-8");
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        // 關閉連接,釋放資源
        if (response != null) {
          response.close();
        }
        if (httpClient != null) {
          httpClient.close();
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return responseContent;
  }
}

以上這篇HttpClient實現調用外部項目接口工具類的示例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/xiaofengyuan/p/6339776.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 男人曰女人 | 调教校花浣肠开菊 | 国产成人久久久精品一区二区三区 | 成人福利在线 | 白丝尤物的下面被疯狂蹂躏 | 高清国产精品久久 | 青青草精品在线观看 | 欧美黑人性猛交╳xx╳动态图 | haodiaocao的视频这里看 | 日出水了特别黄的视频 | 小鸟酱喷水 | 国产精品久久久久久久久久久久久久 | 免费在线公开视频 | 婚色阿花在线全文免费笔 | 特级淫片大乳女子高清视频 | 视频在线观看高清免费 | 欧美激情影音先锋 | 国产视频久久久久 | 91精品国产高清久久久久久io | 国产资源视频在线观看 | 天美视频在线 | 7788理论片在线观看 | 国产成人精品本亚洲 | 精品无人区一区二区三区 | 日韩欧美中文字幕一区二区三区 | 2020年精品国产午夜福利在线 | 国产精品久久久久久久久ktv | 欧美高清国产 | jizz农村野外jizz农民 | 午夜毛片在线观看 | 色老头影视| 国产欧美综合精品一区二区 | 日韩一区视频在线 | 91精品国产综合久久香蕉 | 青草娱乐极品免费视频 | 国产精品制服丝袜白丝www | 色吊丝每日永久访问网站 | 成人伊在线影院 | 114毛片免费观看网站 | 国产一区二区三区欧美 | 婷婷色综合网 |