国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - 使用HttpClient調(diào)用接口的實(shí)例講解

使用HttpClient調(diào)用接口的實(shí)例講解

2021-01-14 16:140001 Java教程

下面小編就為大家?guī)硪黄褂肏ttpClient調(diào)用接口的實(shí)例講解。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

一,編寫返回對象

?
1
2
3
4
5
6
7
8
public class HttpResult {
// 響應(yīng)的狀態(tài)碼
private int code;
 
// 響應(yīng)的響應(yīng)體
private String body;
get/set…
}

二,封裝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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package cn.xxxxxx.httpclient;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;
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 APIService {
 
 private CloseableHttpClient httpClient;
 
 public APIService() {
  // 1 創(chuàng)建HttpClinet,相當(dāng)于打開瀏覽器
  this.httpClient = HttpClients.createDefault();
 }
 
 /**
  * 帶參數(shù)的get請求
  *
  * @param url
  * @param map
  * @return
  * @throws Exception
  */
 public HttpResult doGet(String url, Map<String, Object> map) throws Exception {
 
  // 聲明URIBuilder
  URIBuilder uriBuilder = new URIBuilder(url);
 
  // 判斷參數(shù)map是否為非空
  if (map != null) {
   // 遍歷參數(shù)
   for (Map.Entry<String, Object> entry : map.entrySet()) {
    // 設(shè)置參數(shù)
    uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
   }
  }
 
  // 2 創(chuàng)建httpGet對象,相當(dāng)于設(shè)置url請求地址
  HttpGet httpGet = new HttpGet(uriBuilder.build());
 
  // 3 使用HttpClient執(zhí)行httpGet,相當(dāng)于按回車,發(fā)起請求
  CloseableHttpResponse response = this.httpClient.execute(httpGet);
 
  // 4 解析結(jié)果,封裝返回對象httpResult,相當(dāng)于顯示相應(yīng)的結(jié)果
  // 狀態(tài)碼
  // response.getStatusLine().getStatusCode();
  // 響應(yīng)體,字符串,如果response.getEntity()為空,下面這個(gè)代碼會報(bào)錯(cuò),所以解析之前要做非空的判斷
  // EntityUtils.toString(response.getEntity(), "UTF-8");
  HttpResult httpResult = null;
  // 解析數(shù)據(jù)封裝HttpResult
  if (response.getEntity() != null) {
   httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
     EntityUtils.toString(response.getEntity(), "UTF-8"));
  } else {
   httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
  }
 
  // 返回
  return httpResult;
 }
 
 /**
  * 不帶參數(shù)的get請求
  *
  * @param url
  * @return
  * @throws Exception
  */
 public HttpResult doGet(String url) throws Exception {
  HttpResult httpResult = this.doGet(url, null);
  return httpResult;
 }
 
 /**
  * 帶參數(shù)的post請求
  *
  * @param url
  * @param map
  * @return
  * @throws Exception
  */
 public HttpResult doPost(String url, Map<String, Object> map) throws Exception {
  // 聲明httpPost請求
  HttpPost httpPost = new HttpPost(url);
 
  // 判斷map不為空
  if (map != null) {
   // 聲明存放參數(shù)的List集合
   List<NameValuePair> params = new ArrayList<NameValuePair>();
 
   // 遍歷map,設(shè)置參數(shù)到list中
   for (Map.Entry<String, Object> entry : map.entrySet()) {
    params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
   }
 
   // 創(chuàng)建form表單對象
   UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");
 
   // 把表單對象設(shè)置到httpPost中
   httpPost.setEntity(formEntity);
  }
 
  // 使用HttpClient發(fā)起請求,返回response
  CloseableHttpResponse response = this.httpClient.execute(httpPost);
 
  // 解析response封裝返回對象httpResult
  HttpResult httpResult = null;
  if (response.getEntity() != null) {
   httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
     EntityUtils.toString(response.getEntity(), "UTF-8"));
  } else {
   httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
  }
 
  // 返回結(jié)果
  return httpResult;
 }
 
 /**
  * 不帶參數(shù)的post請求
  *
  * @param url
  * @return
  * @throws Exception
  */
 public HttpResult doPost(String url) throws Exception {
  HttpResult httpResult = this.doPost(url, null);
  return httpResult;
 }
 
 /**
  * 帶參數(shù)的Put請求
  *
  * @param url
  * @param map
  * @return
  * @throws Exception
  */
 public HttpResult doPut(String url, Map<String, Object> map) throws Exception {
  // 聲明httpPost請求
  HttpPut httpPut = new HttpPut(url);
 
  // 判斷map不為空
  if (map != null) {
   // 聲明存放參數(shù)的List集合
   List<NameValuePair> params = new ArrayList<NameValuePair>();
 
   // 遍歷map,設(shè)置參數(shù)到list中
   for (Map.Entry<String, Object> entry : map.entrySet()) {
    params.add(new BasicNameValuePair(entry.getKey(), entry.getValue().toString()));
   }
 
   // 創(chuàng)建form表單對象
   UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "UTF-8");
 
   // 把表單對象設(shè)置到httpPost中
   httpPut.setEntity(formEntity);
  }
 
  // 使用HttpClient發(fā)起請求,返回response
  CloseableHttpResponse response = this.httpClient.execute(httpPut);
 
  // 解析response封裝返回對象httpResult
  HttpResult httpResult = null;
  if (response.getEntity() != null) {
   httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
     EntityUtils.toString(response.getEntity(), "UTF-8"));
  } else {
   httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
  }
 
  // 返回結(jié)果
  return httpResult;
 }
 
 /**
  * 帶參數(shù)的Delete請求
  *
  * @param url
  * @param map
  * @return
  * @throws Exception
  */
 public HttpResult doDelete(String url, Map<String, Object> map) throws Exception {
 
  // 聲明URIBuilder
  URIBuilder uriBuilder = new URIBuilder(url);
 
  // 判斷參數(shù)map是否為非空
  if (map != null) {
   // 遍歷參數(shù)
   for (Map.Entry<String, Object> entry : map.entrySet()) {
    // 設(shè)置參數(shù)
    uriBuilder.setParameter(entry.getKey(), entry.getValue().toString());
   }
  }
 
  // 2 創(chuàng)建httpGet對象,相當(dāng)于設(shè)置url請求地址
  HttpDelete httpDelete = new HttpDelete(uriBuilder.build());
 
  // 3 使用HttpClient執(zhí)行httpGet,相當(dāng)于按回車,發(fā)起請求
  CloseableHttpResponse response = this.httpClient.execute(httpDelete);
 
  // 4 解析結(jié)果,封裝返回對象httpResult,相當(dāng)于顯示相應(yīng)的結(jié)果
  // 狀態(tài)碼
  // response.getStatusLine().getStatusCode();
  // 響應(yīng)體,字符串,如果response.getEntity()為空,下面這個(gè)代碼會報(bào)錯(cuò),所以解析之前要做非空的判斷
  // EntityUtils.toString(response.getEntity(), "UTF-8");
  HttpResult httpResult = null;
  // 解析數(shù)據(jù)封裝HttpResult
  if (response.getEntity() != null) {
   httpResult = new HttpResult(response.getStatusLine().getStatusCode(),
     EntityUtils.toString(response.getEntity(), "UTF-8"));
  } else {
   httpResult = new HttpResult(response.getStatusLine().getStatusCode(), "");
  }
 
  // 返回
  return httpResult;
 }
 
}

三,調(diào)用接口

  1. package cn.xxxxxx.httpclient.test; 
  2.  
  3. import java.util.HashMap; 
  4. import java.util.Map; 
  5.  
  6. import org.junit.Before; 
  7. import org.junit.Test; 
  8.  
  9. import cn.itcast.httpclient.APIService; 
  10. import cn.itcast.httpclient.HttpResult; 
  11.  
  12. public class APIServiceTest { 
  13.  
  14.  private APIService apiService; 
  15.  
  16.  @Before 
  17.  public void init() { 
  18.   this.apiService = new APIService(); 
  19.  } 
  20.  
  21.  // 查詢 
  22.  @Test 
  23.  public void testQueryItemById() throws Exception { 
  24.   // http://manager.aaaaaa.com/rest/item/interface/{id} 
  25.  
  26.   String url = "http://manager.aaaaaa.com/rest/item/interface/42"
  27.  
  28.   HttpResult httpResult = this.apiService.doGet(url); 
  29.  
  30.   System.out.println(httpResult.getCode()); 
  31.   System.out.println(httpResult.getBody()); 
  32.  
  33.  } 
  34.  
  35.  // 新增 
  36.  @Test 
  37.  public void testSaveItem() throws Exception { 
  38.   // http://manager.aaaaaa.com/rest/item/interface/{id} 
  39.  
  40.   String url = "http://manager.aaaaaa.com/rest/item/interface"
  41.  
  42.   Map<String, Object> map = new HashMap<String, Object>(); 
  43.   // title=測試RESTful風(fēng)格的接口&price=1000&num=1&cid=888&status=1 
  44.   map.put("title""測試APIService調(diào)用新增接口"); 
  45.   map.put("price""1000"); 
  46.   map.put("num""1"); 
  47.   map.put("cid""666"); 
  48.   map.put("status""1"); 
  49.  
  50.   HttpResult httpResult = this.apiService.doPost(url, map); 
  51.  
  52.   System.out.println(httpResult.getCode()); 
  53.   System.out.println(httpResult.getBody()); 
  54.  
  55.  } 
  56.  
  57.  // 修改 
  58.  
  59.  @Test 
  60.  public void testUpdateItem() throws Exception { 
  61.   // http://manager.aaaaaa.com/rest/item/interface/{id} 
  62.  
  63.   String url = "http://manager.aaaaaa.com/rest/item/interface"
  64.  
  65.   Map<String, Object> map = new HashMap<String, Object>(); 
  66.   // title=測試RESTful風(fēng)格的接口&price=1000&num=1&cid=888&status=1 
  67.   map.put("title""測試APIService調(diào)用修改接口"); 
  68.   map.put("id""44"); 
  69.  
  70.   HttpResult httpResult = this.apiService.doPut(url, map); 
  71.  
  72.   System.out.println(httpResult.getCode()); 
  73.   System.out.println(httpResult.getBody()); 
  74.  
  75.  } 
  76.  
  77.  // 刪除 
  78.  @Test 
  79.  public void testDeleteItemById() throws Exception { 
  80.   // http://manager.aaaaaa.com/rest/item/interface/{id} 
  81.  
  82.   String url = "http://manager.aaaaaa.com/rest/item/interface/44"
  83.  
  84.   HttpResult httpResult = this.apiService.doDelete(url, null); 
  85.  
  86.   System.out.println(httpResult.getCode()); 
  87.   System.out.println(httpResult.getBody()); 
  88.  
  89.  } 
  90.  

以上這篇使用HttpClient調(diào)用接口的實(shí)例講解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/javaxiaoxin/archive/2017/10/06/7633017.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲精品日本 | 君岛美绪一区二区三区 | 亚洲不卡视频在线观看 | 天堂精品一区二区三区 | 91精品国产综合久久福利 | 成人片免费看 | 免费观看一区二区三区毛片软件 | 国产一区久久 | 热精品| 日本中文字幕免费 | 午夜免费视频网站 | www.欧美| 成人精品视频在线观看 | 欧美一区二区三区精品 | 激情婷婷丁香 | 亚洲一区国产精品 | 中文在线视频 | 日韩欧美中文 | 中文字幕一区二区三区在线视频 | 韩国精品| 在线观看黄免费 | 中文字幕精品一区久久久久 | 亚洲国产免费av | 夜久久| 日本久久精品一区 | 亚洲a网| 日本三级视频在线观看 | 亚洲在线| 亚洲精品国产乱码在线看蜜月 | 91激情视频 | 欧洲色视频 | 国产精品午夜电影 | 国产亚洲精品一区二区 | 午夜播放器在线观看 | 在线日韩一区二区 | 日本中文字幕在线观看 | 国产亚洲精品久久久 | 夜夜爽99久久国产综合精品女不卡 | 日本精品久久 | 日韩精品在线视频观看 | 亚洲精品一区二区三区在线观看 |