httpclient 请求 json 数据
基于\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 数据的更多相关文章
- 关于使用Ajax请求json数据,@RequestMapping返回中文乱码的几种解决办法
一.问题描述: 使用ajax请求json数据的时候,无论如何返回的响应编码都是ISO-8859-1类型,因为统一都是utf-8编码,导致出现返回结果中文乱码情况. $.ajax({ type:&quo ...
- AJAX跨域请求json数据的实现方法
这篇文章介绍了AJAX跨域请求json数据的实现方法,有需要的朋友可以参考一下 我们都知道,AJAX的一大限制是不允许跨域请求. 不过通过使用JSONP来实现.JSONP是一种通过脚本标记注入的方式, ...
- bootstrap通过ajax请求JSON数据后填充到模态框
1. JSP页面中准备模态框 <!-- 详细信息模态框(Modal) --> <div> <div class="modal fade" id=& ...
- react之fetch请求json数据
Fetch下载 npm install whatwg-fetch -S Fetch请求json数据 json文件要放在public内部才能被检索到
- ajax请求json数据案例
今天有这样一个需求,点击六个大洲,出现对应的一些请求信息,展示在下面,请求请求过后,第二次点击就无需请求.如图所示:点击北美洲下面出现请求的一些数据 html代码结构: <div class=& ...
- Struts2 Action接收POST请求JSON数据及其实现解析
一.认识JSON JSON是一种轻量级.基于文本.与语言无关的数据交换格式,可以用文本格式的形式来存储或表示结构化的数据. 二.POST请求与Content-Type: application/jso ...
- jQuery实例之ajax请求json数据案例
今天有这样一个需求,点击六个大洲,出现对应的一些请求信息,展示在下面,请求请求过后,第二次点击就无需请求.如图所示:点击北美洲下面出现请求的一些数据 html代码结构: <div class=& ...
- ajax请求json数据跨域问题(转)
一.后台代理技术 由服务器端向跨域下的网站发出请求,再将请求结果返回给前端,成功避免同源策略的限制. 具体操作如下: 1.在localhost:81/a.html中,向同源下的某个代理程序发出请求 $ ...
- C++通过HTTP请求Get或Post方式请求Json数据(转)
原文网址:https://www.cnblogs.com/shike8080/articles/6549339.html #pragma once#include <iostream>#i ...
随机推荐
- 【2018年12月14日】A股最便宜的股票
新钢股份(SH600782) - 当前便宜指数:193.12 - 滚动扣非市盈率PE:2.91 - 动态市净率PB:0.96 - 动态年化股息收益率:1.75% - 新钢股份(SH600782)的历史 ...
- logback.xml常用配置详解
<?xml version="1.0" encoding="UTF-8"?><configuration debug="false& ...
- Tomcat 8
JDTCompiler.java /** * Compile the jsp file from the current engine context. As an side- effect, * t ...
- Apache安全配置基线指导
搜索关键词:Apache安全配置基线指导 参考链接: windows服务器下Apache 的降权 https://www.landui.com/help/show-1749.html
- Outlook 2007 实现自动添加密送的方法
1)在Outlook里面键入Alt+F11打开VBA编辑器: 2)激活左边的工程面板,展开并双击上面的“Project (VbaProject.OTM)/Microsoft Office Ou ...
- 安装.NET Framework 3.5
https://www.microsoft.com/zh-CN/download/details.aspx?id=22 https://docs.microsoft.com/zh-cn/dotnet/ ...
- 关于VC中的附加进程调试
今天领导要求在服务端添加一个获取会议参数的功能接口,接口写好后要自己测试,但是没有客户端的源码,只有客户端安装程序和客户端与服务端发送信令的底层库KSYSClient.dll,而我修改了客户端需要底层 ...
- 《C++ Primer Plus》16.3 标准模板库 学习笔记
STL提供了一组表示容器.迭代其.函数对象和算法的模板.容器是一个与数组类似的单元,可以存储若干个值.STL容器是同质的,即存储的值的类型相同:算法是完成特定任务(如对数组进行排序或在链表中查找特定值 ...
- 用MyEclipse将Maven Dependencies中的jar包导出
1.右击pom.xml文件,选择Run As ——> Maven build… 2.在打开的页面中,如图输入“dependency:copy-dependencies”,后点击“Run”即可 ...
- 【thinkphp5】使用tp5开发api接口 定义全局异常处理
1 新建文件夹以及文件 路径: /application/lib/exception/ExceptionHandler.php 并键入以下代码 <?php namespace app\lib\e ...