-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsection.apache.httpclient.xml
More file actions
601 lines (524 loc) · 19.7 KB
/
section.apache.httpclient.xml
File metadata and controls
601 lines (524 loc) · 19.7 KB
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
<?xml version="1.0" encoding="UTF-8"?>
<section id="httpclient">
<title>Apache HttpClient</title>
<section id="maven">
<title>Maven</title>
<programlisting>
<![CDATA[
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.netkiller</groupId>
<artifactId>example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
]]>
</programlisting>
</section>
<section id="post">
<title>HTTP POST 操作</title>
<section>
<title>Post Data</title>
<programlisting>
<![CDATA[
package cn.netkiller.httpclient;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.xml.bind.DatatypeConverter;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
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 HttpClientPost {
public static void main(String[] args) throws ClientProtocolException, IOException, NoSuchAlgorithmException {
// TODO Auto-generated method stub
String url = "http://api.netkiller.cn:8080/api/comment/list.jspx";
String appId = "3175755150424665";
String appKey = "yEjnjoSEOQpOP49od1IexLkyVB4HTi9c";
String contentId = "1103";
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String date = dateFormat.format(new Date());
String token = DatatypeConverter.printHexBinary(MessageDigest.getInstance("MD5").digest(String.format("%s&%s", appKey, date).getBytes("UTF-8"))).toLowerCase();
CloseableHttpClient httpclient = HttpClients.createDefault();
// appId=3175755150424665&contentId=1103&pageSize=100&timeStamp=2017-08-03
// 10:20:00&token=e1180c0306aff7792c3e25699900dd0d
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("appId", appId));
params.add(new BasicNameValuePair("contentId", contentId));
params.add(new BasicNameValuePair("pageSize", "10"));
params.add(new BasicNameValuePair("timeStamp", date));
params.add(new BasicNameValuePair("token", token));
System.out.println(params.toString());
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, Consts.UTF_8);
System.out.println(urlEncodedFormEntity.toString());
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(urlEncodedFormEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseBody.toString());
response.close();
}
}
]]>
</programlisting>
</section>
<section>
<title>POST RAW 数据</title>
<programlisting>
<![CDATA[
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
@SuppressWarnings("deprecation")
public class HTTPREST {
public static void main(String[] args) throws ClientProtocolException, IOException {
HttpClient httpClient = new DefaultHttpClient();
try {
HttpPost request = new HttpPost("http://test:[email protected]/v1/test/create.json");
StringEntity params = new StringEntity("{\"name\":\"neo\", \"nickname\":\"netkiller\"}", "UTF-8");
request.addHeader("content-type", "application/json");
request.addHeader("Accept", "application/json");
request.setEntity(params);
HttpResponse response = httpClient.execute(request);
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
if (response != null) {
String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(responseBody.toString());
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
httpClient.getConnectionManager().shutdown();
}
}
}
]]>
</programlisting>
</section>
<section id="httpclient.post.gbk">
<title>POST GBK 编码得数据</title>
<para>有些国内短信运营商发送短信的接口只能接收 GBK 编码,下面就是一个POST GBK编码数据的范例。</para>
<programlisting>
<![CDATA[
package api.test.sms;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
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 SMS {
public SMS() {
// TODO Auto-generated constructor stub
}
public void sendSMS(String mobile, String message) throws ClientProtocolException, IOException {
String url = "http://api.netkiller.cn/sms/v2/send";
SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
String timestamp = sdf.format(Calendar.getInstance().getTime());
CloseableHttpClient httpclient = HttpClients.createDefault();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("apikey", "2863300994052170feb"));
params.add(new BasicNameValuePair("mobile", mobile));
params.add(new BasicNameValuePair("content", message));
UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(params, "GBK");
System.out.println(urlEncodedFormEntity.toString());
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(urlEncodedFormEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity, "UTF-8");
System.out.println(params.toString());
System.out.println(response.getStatusLine());
System.out.println(responseBody.toString());
response.close();
}
public static void main(String[] args) throws ClientProtocolException, IOException {
// TODO Auto-generated method stub
SMS sms = new SMS();
String message = String.format("您的验证码是%s,在%s分钟内输入有效。如非本人操作请忽略此短信。", 8888, 10);
sms.sendSMS("13113668800", message);
}
}
]]>
</programlisting>
</section>
</section>
<section id="ssl">
<title>HTTPS</title>
<section>
<title>Get https 接口</title>
<para>环境 Nginx SSL(openssl自颁发),nginx 通过proxy_pass连接 Tomcat</para>
<para>下面是 nginx 配置</para>
<screen>
<![CDATA[
server {
listen 443 ssl spdy;
server_name api.netkiller.cn;
ssl_certificate /etc/nginx/ssl/api.netkiller.cn.crt;
ssl_certificate_key /etc/nginx/ssl/api.netkiller.cn.key;
ssl_session_cache shared:SSL:20m;
ssl_session_timeout 60m;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
charset utf-8;
access_log /var/log/nginx/api.netkiller.cn.access.log;
error_log /var/log/nginx/api.netkiller.cn.error.log;
location / {
proxy_pass http://127.0.0.1:7000;
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For
$proxy_add_x_forwarded_for;
}
}
]]>
</screen>
<para>下面是 Java 程序</para>
<programlisting>
<![CDATA[
package cn.netkiller.example;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
public class NginxAndOpenSSLAndTomcatAndHttpclient {
public static void main(String[] args) throws ParseException, IOException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(builder.build());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslFactory).build();
HttpGet httpGet = new HttpGet("https://neo:[email protected]/v1/news/today.json");
CloseableHttpResponse response = httpclient.execute(httpGet);
try {
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseBody.toString());
} finally {
response.close();
}
}
}
]]>
</programlisting>
<para>
如果遇到配置问题,可以看一下
<ulink url="/www/index.html">《Netkiller Linux Web 手札》</ulink>
</para>
<!-- /* static { System.setProperty("javax.net.ssl.trustStore","/www/nginx/client1.jks"); System.setProperty("javax.net.ssl.trustStorePassword", "password"); System.setProperty("javax.net.ssl.keyStore", "/www/nginx/client1.jks"); System.setProperty("javax.net.ssl.keyStorePassword", "password"); } */ -->
</section>
<section>
<title>POST json 数据</title>
<programlisting>
<![CDATA[
package cn.netkiller.example;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;
public class HttpClientSSLPost {
public HttpClientSSLPost() {
// TODO Auto-generated constructor stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SSLContextBuilder builder = new SSLContextBuilder();
try {
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslFactory = new SSLConnectionSocketFactory(builder.build());
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslFactory).build();
HttpPost httpPost = new HttpPost("https://neo:[email protected]/v1/member/create.json");
httpPost.addHeader("content-type", "application/json");
httpPost.addHeader("Accept", "application/json");
HttpEntity httpEntity = new StringEntity("{\"name\":\"neo\", \"nickname\":\"netkiler\",\"age\":\"18\"}", "UTF-8");
httpPost.setEntity(httpEntity);
CloseableHttpResponse response = httpclient.execute(httpPost);
System.out.println(response.getStatusLine());
HttpEntity entity = response.getEntity();
String responseBody = EntityUtils.toString(entity, "UTF-8");
System.out.println(responseBody.toString());
response.close();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyStoreException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
}
}
}
]]>
</programlisting>
</section>
</section>
<section id="httpclient.http2">
<title>HTTP/2</title>
<para>HTTP/2实例</para>
<programlisting>
<![CDATA[
@Test
public void testHttp2() throws URISyntaxException, IOException, InterruptedException {
HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.SECURE)
.version(HttpClient.Version.HTTP_2)
.build()
.sendAsync(HttpRequest.newBuilder()
.uri(new URI("https://http2.akamai.com/demo"))
.GET()
.build(),
HttpResponse.BodyHandler.asString())
.whenComplete((resp,t) -> {
if(t != null){
t.printStackTrace();
}else{
System.out.println(resp.body());
System.out.println(resp.statusCode());
}
}).join();
}
]]>
</programlisting>
</section>
<section id="httpclient.java11">
<title>Java11</title>
<section>
<title>sync get</title>
<screen>
<![CDATA[
/**
* --add-modules jdk.incubator.httpclient
* @throws IOException
* @throws InterruptedException
* @throws URISyntaxException
*/
@Test
public void testGet() throws IOException, InterruptedException, URISyntaxException {
HttpClient httpClient = HttpClient.newHttpClient();
HttpRequest httpRequest = HttpRequest.newBuilder()
.uri(new URI("https://www.baidu.com"))
.header("User-Agent", "jdk 9 http client")
.GET()
.build();
HttpResponse<String> httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandler.asString());
System.out.println(httpResponse.statusCode());
System.out.println(httpResponse.body());
}
]]>
</screen>
</section>
<section>
<title>async get</title>
<screen>
<![CDATA[
@Test
public void testAsyncGet() throws URISyntaxException, InterruptedException, ExecutionException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("https://www.baidu.com"))
.GET()
.build();
CompletableFuture<HttpResponse<String>> response = client.sendAsync(request, HttpResponse.BodyHandler.asString());
response.whenComplete((resp,t) -> {
if(t != null){
t.printStackTrace();
}else{
System.out.println(resp.body());
System.out.println(resp.statusCode());
}
}).join();
}
]]>
</screen>
</section>
<section>
<title>post form</title>
<para>post form</para>
<screen>
<![CDATA[
@Test
public void testPostForm() throws URISyntaxException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(new URI("http://www.w3school.com.cn/demo/demo_form.asp"))
.header("Content-Type","application/x-www-form-urlencoded")
.POST(HttpRequest.BodyProcessor.fromString("name1=value1&name2=value2"))
.build();
client.sendAsync(request, HttpResponse.BodyHandler.asString())
.whenComplete((resp,t) -> {
if(t != null){
t.printStackTrace();
}else{
System.out.println(resp.body());
System.out.println(resp.statusCode());
}
}).join();
}
]]>
</screen>
</section>
</section>
<section id="httpclient.faq">
<title>Host name 'api.netkiller.cn' does not match the certificate subject provided</title>
<para>这个问题是CN不匹配造成的,日志如下</para>
<screen>
<![CDATA[
Exception in thread "main" javax.net.ssl.SSLPeerUnverifiedException: Host name 'api.netkiller.cn' does not match the certificate subject provided by the peer ([email protected], CN=netkiller.cn, OU=CF, O=CF, L=Shenzhen, ST=Guangdong, C=CN)
]]>
</screen>
<para>解决方案一:重新做证书</para>
<screen>
<![CDATA[
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:Guangdong
Locality Name (eg, city) [Default City]:Shenzhen
Organization Name (eg, company) [Default Company Ltd]:CF
Organizational Unit Name (eg, section) []:CF
Common Name (eg, your name or your server's hostname) []:api.netkiller.cn
Email Address []:[email protected]
]]>
</screen>
<para>解决方案二:是用CN上的域名链接</para>
</section>
<section id="httpclient.HttpStatus">
<title>HttpStatus</title>
<programlisting>
<![CDATA[
package cn.netkiller.consul.controller;
import org.apache.http.HttpStatus;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
public TestController() {
// TODO Auto-generated constructor stub
}
@RequestMapping("/health")
public int health() {
return HttpStatus.SC_OK;
}
}
]]>
</programlisting>
</section>
<section>
<title></title>
<programlisting>
<![CDATA[
String url = "https://www.netkiller.cn:8080/book/chapter1/section2?id=9527";
URIBuilder uriBuilder = new URIBuilder(url);
System.out.println(uriBuilder.getScheme());
System.out.println(uriBuilder.getUserInfo());
System.out.println(uriBuilder.getHost());
System.out.println(uriBuilder.getPort());
System.out.println(uriBuilder.getPath());
System.out.println(uriBuilder.getQueryParams());
System.out.println(uriBuilder.getFragment());
System.out.println(uriBuilder.getCharset());
]]>
</programlisting>
<para>添加参数</para>
<programlisting>
<![CDATA[
String url = "https://www.netkiller.cn/test?id=1";
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.setParameter("id", "2"); // set 会覆盖原来的参数
uriBuilder.addParameter("cat" ,"5"); // add 添加参数
System.out.println(uriBuilder.build());
]]>
</programlisting>
<para>去除参数</para>
<programlisting>
<![CDATA[
String url = "https://www.netkiller.cn:8080/book/chapter1/section2?id=9527";
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.clearParameters();
System.out.println(uriBuilder.build());
# https://www.netkiller.cn:8080/book/chapter1/section2
]]>
</programlisting>
</section>
</section>