Utils--前台调用后台接口工具类
Utils--前台调用后台接口工具类
package com.taotao.manage.httpclient; import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map; import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
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.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class ApiHttpClientService implements BeanFactoryAware { private BeanFactory beanFactory; @Autowired(required=false)
private RequestConfig config; /**
* GET请求,成功返回String;失败返回null
*
* @param url
* @return
* @throws ParseException
* @throws IOException
*/
public String doGet(String url) throws ParseException, IOException {
// 2、创建http GET请求
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(config);
CloseableHttpResponse response = null;
try {
// 3、执行请求
response = getttpClient().execute(httpGet);
// 4、判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
return EntityUtils.toString(response.getEntity(), "UTF-8");
}
} finally {
if (response != null) {
response.close();
}
}
return null;
} /**
* 带有参数的GET请求 ,返回:null,请求失败,String数据,请求成功
*
* @param url
* @param params
* @return
* @throws URISyntaxException
* @throws IOException
* @throws ParseException
*/
public String doGet(String url, Map<String, String> params) throws URISyntaxException, ParseException, IOException {
URIBuilder uriBuilder = new URIBuilder(url);
for (Map.Entry<String, String> map : params.entrySet()) {
uriBuilder.setParameter(map.getKey(), map.getValue());
}
return this.doGet(uriBuilder.build().toURL().toString());
} /**
* 创建http POST请求
*
* @param url
* @param params
* @return
* @throws IOException
* @throws ParseException
*/
public HttpResult doPost(String url, Map<String, String> params) throws ParseException, IOException {
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(config);
// 伪装成浏览器访问
// 设置2个post参数,一个是scope、一个是q
if (params != null) {
List<NameValuePair> parameters = new ArrayList<NameValuePair>(0);
for (Map.Entry<String, String> map : params.entrySet()) {
parameters.add(new BasicNameValuePair(map.getKey(), map.getValue()));
}
// 构造一个form表单式的实体
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters);
// 将请求实体设置到httpPost对象中
httpPost.setEntity(formEntity);
}
CloseableHttpResponse response = null;
try {
// 执行请求
response = getttpClient().execute(httpPost);
// 判断返回状态是否为200
return new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(), "UTF-8"));
} finally {
if (response != null) {
response.close();
}
}
} /**
* POST无餐请求
*
* @param url
* @return
* @throws ParseException
* @throws IOException
*/
public HttpResult doPost(String url) throws ParseException, IOException {
return this.doPost(url, null);
} /**
* 拿到最新的,是多例的
*
* @return
*/
public CloseableHttpClient getttpClient() {
return this.beanFactory.getBean(CloseableHttpClient.class);
} /**
* spring/applicationContext-HttpClient为多例 创建多例 ,可以放对象
*
* @param bean
* @throws BeansException
*/
@Override
public void setBeanFactory(BeanFactory bean) throws BeansException {
this.beanFactory = bean;
} /**
* 创建http POST请求json
*
* @param url
* @param params
* @return
* @throws IOException
* @throws ParseException
*/
public HttpResult doPostJSON(String url, String params) throws ParseException, IOException {
HttpPost httpPost = new HttpPost(url);
httpPost.setConfig(config);
// 伪装成浏览器访问
// 设置2个post参数,一个是scope、一个是q
if (params != null) {
StringEntity entity=new StringEntity(params, ContentType.APPLICATION_JSON);
// 将请求实体设置到httpPost对象中
httpPost.setEntity(entity);
}
CloseableHttpResponse response = null;
try {
// 执行请求
response = getttpClient().execute(httpPost);
// 判断返回状态是否为200
return new HttpResult(response.getStatusLine().getStatusCode(),
EntityUtils.toString(response.getEntity(), "UTF-8"));
} finally {
if (response != null) {
response.close();
}
}
}
}
好久之前写的,今天忽然有个想法,把这些工具类总计下。以便以后的使用
Utils--前台调用后台接口工具类的更多相关文章
- Java调用第三方接口工具类(json、form)
1.JSON值访问 /** * 调用对方接口方法 * @param path 对方或第三方提供的路径 * @param data 向对方或第三方发送的数据,大多数情况下给对方发送JSON数据让对方解析 ...
- Java模拟http请求调用远程接口工具类
package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...
- 带SSL证书的httpclient 远程接口工具类
package com.iups.wx.util; import java.io.IOException; import java.io.UnsupportedEncodingException; i ...
- thinkjs学习-this.assign传递数据和ajax调用后台接口
在页面加载时,就需要显示在页面上的数据,可以在后台使用this.assign赋值,在前台通过ejs等模板获取:用户点击按钮,或者触发某些事件和后台进行交互时,就需要用到ajax调用后台接口.本文通过一 ...
- Asp.Net前台调用后台变量
1.Asp.Net中几种相似的标记符号: < %=...%>< %#... %>< % %>< %@ %>解释及用法 答: < %#... %&g ...
- 由ASP.NET所谓前台调用后台、后台调用前台想到HTTP——实践篇(二)
在由ASP.NET所谓前台调用后台.后台调用前台想到HTTP——理论篇中描述了一下ASP.NET新手的三个问题及相关的HTTP协议内容,在由ASP.NET所谓前台调用后台.后台调用前台想到HTTP—— ...
- 由ASP.NET所谓前台调用后台、后台调用前台想到HTTP——理论篇
工作两年多了,我会经常尝试给公司小伙伴儿们解决一些问题,几个月下来我发现初入公司的小朋友最爱问的问题就三个 1. 我想前台调用后台的XXX方法怎么弄啊? 2. 我想后台调用前台的XXX JavaScr ...
- 由ASP.NET所谓前台调用后台、后台调用前台想到HTTP
由ASP.NET所谓前台调用后台.后台调用前台想到HTTP 在由ASP.NET所谓前台调用后台.后台调用前台想到HTTP——理论篇中描述了一下ASP.NET新手的三个问题及相关的HTTP协议内容,在由 ...
- ASP.NET所谓前台调用后台、后台调用前台想到HTTP——实践篇
由ASP.NET所谓前台调用后台.后台调用前台想到HTTP——实践篇 在由ASP.NET所谓前台调用后台.后台调用前台想到HTTP——理论篇中描述了一下ASP.NET新手的三个问题及相关的HTTP协议 ...
随机推荐
- 【NOI 2016】优秀的拆分
Problem Description 如果一个字符串可以被拆分为 \(AABB\) 的形式,其中 \(A\) 和 \(B\) 是任意非空字符串,则我们称该字符串的这种拆分是优秀的. 例如,对于字符串 ...
- JavaScript知识点整理
1.JavaScript的定义 JavaScript是一种专门为与网页交互而设计的脚本语言.有下列三部分组成 ①ECMAScript,提供核心语言功能 ②文档对象模型(DOM),提供访问与操作网页内容 ...
- snakemake使用笔记
snakemake是一个用来编写任务流程的工具,用python编写的,因此其执行的流程脚本也比较通俗易懂,易于理解. 一.从一个简单的例子开始 1.安装snakemake 安装snakemake的方法 ...
- 【Python】【Web开发】
# [[Web开发]] ''' 最早的软件都是运行在大型机上的,软件使用者通过“哑终端”登陆到大型机上去运行软件.后来随着PC机的兴起,软件开始主要运行在桌面上,而数据库这样的软件运行在服务器端,这种 ...
- 下载安装Android sdk tools
安装java: https://www.cnblogs.com/sea-stream/p/5815957.html 下载地址:https://www.androiddevtools.cn/ 选择版本 ...
- async、await在ASP.NET[ MVC]中之线程死锁的故事
场景重构 public ActionResult Index(string ucode) { string userInfo = GetUserInfo(ucode).Result; ViewData ...
- 微信小程序 数据绑定方式
与vue不同,在微信小程序中,js的数据和前端显示的数据是单数据流,也就是说,js里边的数据变了(通过setData),前端能立刻显示.但如果前端数据变了,js中的变量不能改变. 这个相比传统的前端已 ...
- js常见知识点2.面向对象相关
一.对象的概念 建议回复: 对象是一个整体,对外提供一些功能. 一切具有属性和方法的事物. 一切具有本质特征和行为的物质. 数据类型: 所有的基本数据类型都没有属性和方法. 所 ...
- Asp.net core 学习笔记 (Excel 读写)
EPPlus 已经支持 .net core 了 https://www.nuget.org/packages/EPPlus https://github.com/JanKallman/EPPlus 写 ...
- python+selenium2(二)
看完第一个程序,可能有不懂得地方,里面有定位元素的方式,之后会具体介绍定位的方式.这一篇介绍下对浏览器的操作. (1)浏览器的最大化 有点问题, Message: unknown error: c ...