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

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

json api接口地址:

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

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

测试代码:

 import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils; import com.sun.java.swing.plaf.windows.resources.windows; public class HttpServletUtil { String result = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).build(); public String doPost(String params, String url) throws Exception { HttpPost httpPost = new HttpPost(url);
StringEntity entity = new StringEntity(params.toString(), "utf-8");
httpPost.setEntity(entity);
//设置请求和传输超时时间
httpPost.setConfig(requestConfig);
CloseableHttpResponse httpResp = httpclient.execute(httpPost);
try {
int statusCode = httpResp.getStatusLine().getStatusCode();
// 判断是够请求成功
if (statusCode == HttpStatus.SC_OK) {
System.out.println("状态码:" + statusCode);
System.out.println("请求成功!");
// 获取返回的数据
result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
} else {
System.out.println("状态码:"
+ httpResp.getStatusLine().getStatusCode());
System.out.println("HttpPost方式请求失败!");
}
} finally {
httpResp.close();
httpclient.close();
}
return result;
} public String doGet(String url) throws Exception{
String result = null;
CloseableHttpClient httpclient = HttpClients.createDefault();
httpclient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
//设置请求和传输超时时间
httpGet.setConfig(requestConfig);
CloseableHttpResponse httpResp = httpclient.execute(httpGet);
try {
int statusCode = httpResp.getStatusLine().getStatusCode();
// 判断是够请求成功
if (statusCode == HttpStatus.SC_OK) {
System.out.println("状态码:" + statusCode);
System.out.println("请求成功!");
// 获取返回的数据
result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
} else {
System.out.println("状态码:"
+ httpResp.getStatusLine().getStatusCode());
System.out.println("HttpGet方式请求失败!");
}
} finally {
httpResp.close();
httpclient.close();
}
return result;
} public static void main(String args[]) throws Exception{ //String url = "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi";
//String params = "scope=103&format=json&appid=379020&bk_key=关键字&bk_length=600";
//String s = HttpServletUtil.doPost(params, url); String url = "http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key=关键字&bk_length=600";
String s = new HttpServletUtil().doGet(url);
System.out.println(s);
} }

请求成功后如下

httpclient 请求 json 数据的更多相关文章

  1. 关于使用Ajax请求json数据,@RequestMapping返回中文乱码的几种解决办法

    一.问题描述: 使用ajax请求json数据的时候,无论如何返回的响应编码都是ISO-8859-1类型,因为统一都是utf-8编码,导致出现返回结果中文乱码情况. $.ajax({ type:&quo ...

  2. AJAX跨域请求json数据的实现方法

    这篇文章介绍了AJAX跨域请求json数据的实现方法,有需要的朋友可以参考一下 我们都知道,AJAX的一大限制是不允许跨域请求. 不过通过使用JSONP来实现.JSONP是一种通过脚本标记注入的方式, ...

  3. bootstrap通过ajax请求JSON数据后填充到模态框

    1.   JSP页面中准备模态框 <!-- 详细信息模态框(Modal) --> <div> <div class="modal fade" id=& ...

  4. react之fetch请求json数据

    Fetch下载 npm install whatwg-fetch -S Fetch请求json数据 json文件要放在public内部才能被检索到

  5. ajax请求json数据案例

    今天有这样一个需求,点击六个大洲,出现对应的一些请求信息,展示在下面,请求请求过后,第二次点击就无需请求.如图所示:点击北美洲下面出现请求的一些数据 html代码结构: <div class=& ...

  6. Struts2 Action接收POST请求JSON数据及其实现解析

    一.认识JSON JSON是一种轻量级.基于文本.与语言无关的数据交换格式,可以用文本格式的形式来存储或表示结构化的数据. 二.POST请求与Content-Type: application/jso ...

  7. jQuery实例之ajax请求json数据案例

    今天有这样一个需求,点击六个大洲,出现对应的一些请求信息,展示在下面,请求请求过后,第二次点击就无需请求.如图所示:点击北美洲下面出现请求的一些数据 html代码结构: <div class=& ...

  8. ajax请求json数据跨域问题(转)

    一.后台代理技术 由服务器端向跨域下的网站发出请求,再将请求结果返回给前端,成功避免同源策略的限制. 具体操作如下: 1.在localhost:81/a.html中,向同源下的某个代理程序发出请求 $ ...

  9. C++通过HTTP请求Get或Post方式请求Json数据(转)

    原文网址:https://www.cnblogs.com/shike8080/articles/6549339.html #pragma once#include <iostream>#i ...

随机推荐

  1. 【QT】error: Failed to retrieve MSVC Environment from "\VC\vcvarsall.bat":

    安装QT后直接打开,报错. 需要删mysql环境变量. error: Failed to retrieve MSVC Environment from "D:\Englishpath\VS2 ...

  2. classpath路劲

    1.指的是tomcat下的web-if下的classes路劲 2. 发布时,红框中的文件全都会放到classes下,并且如果出现重名,下面的有可能会覆盖上面的文件

  3. rpc框架thrift

    跨语言的rpc框架 新建一个thrift文件 # ping service demoservice PingService { string ping(), ping函数的返回类型是字符串} serv ...

  4. SpringMVC -- 梗概--源码--壹--跳转

    1.配置web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version=&qu ...

  5. JAVA WEB ------ 文件下载及导出数据到office Execl表格

    文件下载需要五步: 1.设置文件ContentType类型 // 设置文件ContentType类型,这样设置,会自动判断下载文件类型 response.setContentType("mu ...

  6. 【Android】amr播放

    http://download.csdn.net/download/r8hzgemq/4877495 http://www.cnblogs.com/fengzhblog/archive/2013/08 ...

  7. 【代码审计】LaySNS_v2.2.0 前台XSS跨站脚本漏洞

      0x00 环境准备 LaySNS官网:http://www.laysns.com/ 网站源码版本:LaySNS_v2.2.0 程序源码下载:https://pan.lanzou.com/i0l38 ...

  8. linux 查看硬件信息

      1.查看内存槽数.那个槽位插了内存,大小是多少 dmidecode|grep -P -A5 "Memory\s+Device"|grep Size|grep -v Range ...

  9. Android的Fragment中onActivityResult不被调用

    1.检查该Fragment所属的Activity中,是否重写了onActivityResult方法. 2.检查Fragment中的startActivityForResult的调用方式. 请确保不要使 ...

  10. Oracle和Mysql中mybatis模糊匹配查询区别

    1.Oracle AND NAME LIKE '%'||#{name}||'%' 2.Mysql AND NAME LIKE "%"#{name}"%"