httpclient 请求 json 数据

Posted double2014

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了httpclient 请求 json 数据相关的知识,希望对你有一定的参考价值。

基于\\httpcomponents-client-4.5.5需要引入相关jar包如下:

必须导入commons-logging-1.2.jar,否则会提示

 

json api接口地址:

https://www.bejson.com/knownjson/webInterface/

本例用了百度上的那个接口

测试代码:

 1 import org.apache.http.HttpStatus;
 2 import org.apache.http.client.config.RequestConfig;
 3 import org.apache.http.client.methods.CloseableHttpResponse;
 4 import org.apache.http.client.methods.HttpGet;
 5 import org.apache.http.client.methods.HttpPost;
 6 import org.apache.http.entity.StringEntity;
 7 import org.apache.http.impl.client.CloseableHttpClient;
 8 import org.apache.http.impl.client.HttpClients;
 9 import org.apache.http.util.EntityUtils;
10 
11 import com.sun.java.swing.plaf.windows.resources.windows;
12 
13 
14 public class HttpServletUtil {
15     
16     String result = null;
17     CloseableHttpClient httpclient = HttpClients.createDefault();
18     RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build();
19     
20     public String doPost(String params, String url) throws Exception {
21         
22         HttpPost httpPost = new HttpPost(url);
23         StringEntity entity = new StringEntity(params.toString(), "utf-8");
24         httpPost.setEntity(entity);
25         //设置请求和传输超时时间
26         httpPost.setConfig(requestConfig);
27         CloseableHttpResponse httpResp = httpclient.execute(httpPost);
28         try {
29             int statusCode = httpResp.getStatusLine().getStatusCode();
30             // 判断是够请求成功
31             if (statusCode == HttpStatus.SC_OK) {
32                 System.out.println("状态码:" + statusCode);
33                 System.out.println("请求成功!");
34                 // 获取返回的数据
35                 result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
36             } else {
37                 System.out.println("状态码:"
38                         + httpResp.getStatusLine().getStatusCode());
39                 System.out.println("HttpPost方式请求失败!");
40             }
41         } finally {
42             httpResp.close();
43             httpclient.close();
44         }
45         return result;
46     }
47 
48     public String doGet(String url) throws Exception{
49         String result = null;
50         CloseableHttpClient httpclient = HttpClients.createDefault();
51         httpclient = HttpClients.createDefault();
52         HttpGet httpGet = new HttpGet(url);
53         //设置请求和传输超时时间
54         httpGet.setConfig(requestConfig);
55         CloseableHttpResponse httpResp = httpclient.execute(httpGet);
56         try {
57             int statusCode = httpResp.getStatusLine().getStatusCode();
58             // 判断是够请求成功
59             if (statusCode == HttpStatus.SC_OK) {
60                 System.out.println("状态码:" + statusCode);
61                 System.out.println("请求成功!");
62                 // 获取返回的数据
63                 result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
64             } else {
65                 System.out.println("状态码:"
66                         + httpResp.getStatusLine().getStatusCode());
67                 System.out.println("HttpGet方式请求失败!");
68             }
69         } finally {
70             httpResp.close();
71             httpclient.close();
72         }
73         return result;
74      }
75     
76     public static void main(String args[]) throws Exception{
77         
78         //String url = "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi";
79         //String params = "scope=103&format=json&appid=379020&bk_key=关键字&bk_length=600";
80         //String s = HttpServletUtil.doPost(params, url);
81         
82         String url = "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=关键字&bk_length=600";
83         String s = new HttpServletUtil().doGet(url);
84         System.out.println(s);
85     }
86 
87     
88 }
View Code

 

请求成功后如下

 

以上是关于httpclient 请求 json 数据的主要内容,如果未能解决你的问题,请参考以下文章

httpclient get请求返回的数据乱码?跪求大神帮帮我..下面是代码,就是一个调用接口返回天气预报json数据

httpclient模拟post请求json封装表单数据

若不用jsonp,可使用httpClient请求数据

如何通过 HttpClient 在 POST 请求中将 JSON 数据作为正文发送

跨域请求httpclient

httpclient能通过get类型请求发送json数据吗?