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

 

请求成功后如下

 

转载于:https://www.cnblogs.com/double405/p/8445948.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值