Commit dca65fc21ed763b7c78b7f6234e083c9c09ecc5d
1 parent
d244a372
u
Showing
1 changed file
with
18 additions
and
4 deletions
00.公共组件/1.3sdk.md
| @@ -3,9 +3,21 @@ | @@ -3,9 +3,21 @@ | ||
| 3 | 3 | ||
| 4 | package com.cnlive.open.sdk; | 4 | package com.cnlive.open.sdk; |
| 5 | 5 | ||
| 6 | +import java.io.IOException; | ||
| 7 | + | ||
| 6 | import java.util.HashMap; | 8 | import java.util.HashMap; |
| 7 | import java.util.Map; | 9 | import java.util.Map; |
| 8 | 10 | ||
| 11 | +import org.apache.http.NameValuePair; | ||
| 12 | +import org.apache.http.client.entity.UrlEncodedFormEntity; | ||
| 13 | +import org.apache.http.client.methods.CloseableHttpResponse; | ||
| 14 | +import org.apache.http.client.methods.HttpUriRequest; | ||
| 15 | +import org.apache.http.client.methods.RequestBuilder; | ||
| 16 | +import org.apache.http.impl.client.CloseableHttpClient; | ||
| 17 | +import org.apache.http.impl.client.HttpClients; | ||
| 18 | +import org.apache.http.message.BasicNameValuePair; | ||
| 19 | +import org.apache.http.util.EntityUtils; | ||
| 20 | + | ||
| 9 | public class Example { | 21 | public class Example { |
| 10 | 22 | ||
| 11 | public static void main(String[] args) { | 23 | public static void main(String[] args) { |
| @@ -35,14 +47,16 @@ public class Example { | @@ -35,14 +47,16 @@ public class Example { | ||
| 35 | //使用apache HttpClient 工具调用。 httpclient 4.3.6 | 47 | //使用apache HttpClient 工具调用。 httpclient 4.3.6 |
| 36 | CloseableHttpClient httpClient = HttpClients.createDefault(); | 48 | CloseableHttpClient httpClient = HttpClients.createDefault(); |
| 37 | try { | 49 | try { |
| 38 | - RequestBuilder builder = RequestBuilder.post().setUri(url); | ||
| 39 | - List<NameValuePair> params = new ArrayList<NameValuePair>(); | 50 | + List<NameValuePair> params = new ArrayList<NameValuePair>(); |
| 40 | for (String k : map.keySet()) { | 51 | for (String k : map.keySet()) { |
| 41 | params.add(new BasicNameValuePair(k,map.get(k))); | 52 | params.add(new BasicNameValuePair(k,map.get(k))); |
| 42 | } | 53 | } |
| 43 | 54 | ||
| 44 | - builder.setEntity(new UrlEncodedFormEntity(params,"UTF-8")); | ||
| 45 | - CloseableHttpResponse rep = httpClient.execute(builder.build()); | 55 | + HttpUriRequest httpUriRequest = RequestBuilder.post() |
| 56 | + .setUri(url) | ||
| 57 | + .setEntity(new UrlEncodedFormEntity(params,"UTF-8")) | ||
| 58 | + .build(); | ||
| 59 | + CloseableHttpResponse rep = httpClient.execute(httpUriRequest); | ||
| 46 | try { | 60 | try { |
| 47 | //输出,或转化为java bean对象 | 61 | //输出,或转化为java bean对象 |
| 48 | System.out.println(EntityUtils.toString(rep.getEntity())); | 62 | System.out.println(EntityUtils.toString(rep.getEntity())); |