基于\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. java用substring函数截取string中一段字符串

    在String中有两个substring()函数,如下: 一:String.substring(int start) 参数: start:要截取位置的索引 返回: 从start开始到结束的字符串 例如 ...

  2. [OpenCV] Samples 15: Background Subtraction and Gaussian mixture models

    不错的草稿.但进一步处理是必然的,也是难点所在. Extended: 固定摄像头,采用Gaussian mixture models对背景建模. OpenCV 中实现了两个版本的高斯混合背景/前景分割 ...

  3. flexbox父盒子flex-wrap属性

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. grid网格的流动一

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. ios开发之--sizeToFit的用法

    sizeToFit :即当前视图便捷和便捷大小变化(自动根据文本大小改变自身的宽度) 代码如下: - (void)sizeToFitDemo { UILabel * label = [[UILabel ...

  6. JavaScript-this理解

    javascript this可以绑定到:全局对象,自己定义的对象,用构造函数生成的对象,通过call或者apply更改绑定的对象    1.全局对象  function globalTest(nam ...

  7. 导入贴图操作:处理贴图MaxSize和Format

    using UnityEngine; using System.Collections; using UnityEditor; public class ImportModflyTextures : ...

  8. windows内核情景分析之—— KeRaiseIrql函数与KeLowerIrql()函数

    windows内核情景分析之—— KeRaiseIrql函数与KeLowerIrql()函数 1.KeRaiseIrql函数 这个 KeRaiseIrql() 只是简单地调用 hal 模块的 KfRa ...

  9. echarts.js多图表数据展示使用小结

    echarts api文档: http://echarts.baidu.com/echarts2/doc/doc.html echarts demo示例: http://echarts.baidu.c ...

  10. <转>Python: __init__.py 用法

    转自 http://www.cnblogs.com/BeginMan/p/3183629.html python的每个模块的包中,都有一个__init__.py文件,有了这个文件,我们才能导入这个目录 ...