zoukankan      html  css  js  c++  java
  • 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

    请求成功后如下

  • 相关阅读:
    LUA脚本中的方法使用冒号和点,以及调用者使用冒号和点
    Lua类对象的继承
    Lua类对象和类对象的单例
    toLua使用protobuf协议转Lua表
    关于Lua表的弱引用
    Lua-面向对象中函数使用时冒号(:)和点(.)的区别
    Python【day 14-5】sorted filter map函数应用和练习
    Python【day 14-4】sorted filter map+递归文件夹+二分法查找
    Python【day 14-3】二分法查找
    Python【day 14-2】递归遍历文件夹
  • 原文地址:https://www.cnblogs.com/double405/p/8445948.html
Copyright © 2011-2022 走看看