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--前台调用后台接口工具类的更多相关文章

  1. Java调用第三方接口工具类(json、form)

    1.JSON值访问 /** * 调用对方接口方法 * @param path 对方或第三方提供的路径 * @param data 向对方或第三方发送的数据,大多数情况下给对方发送JSON数据让对方解析 ...

  2. Java模拟http请求调用远程接口工具类

    package ln; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamRea ...

  3. 带SSL证书的httpclient 远程接口工具类

    package com.iups.wx.util; import java.io.IOException; import java.io.UnsupportedEncodingException; i ...

  4. thinkjs学习-this.assign传递数据和ajax调用后台接口

    在页面加载时,就需要显示在页面上的数据,可以在后台使用this.assign赋值,在前台通过ejs等模板获取:用户点击按钮,或者触发某些事件和后台进行交互时,就需要用到ajax调用后台接口.本文通过一 ...

  5. Asp.Net前台调用后台变量

    1.Asp.Net中几种相似的标记符号: < %=...%>< %#... %>< % %>< %@ %>解释及用法 答: < %#... %&g ...

  6. 由ASP.NET所谓前台调用后台、后台调用前台想到HTTP——实践篇(二)

    在由ASP.NET所谓前台调用后台.后台调用前台想到HTTP——理论篇中描述了一下ASP.NET新手的三个问题及相关的HTTP协议内容,在由ASP.NET所谓前台调用后台.后台调用前台想到HTTP—— ...

  7. 由ASP.NET所谓前台调用后台、后台调用前台想到HTTP——理论篇

    工作两年多了,我会经常尝试给公司小伙伴儿们解决一些问题,几个月下来我发现初入公司的小朋友最爱问的问题就三个 1. 我想前台调用后台的XXX方法怎么弄啊? 2. 我想后台调用前台的XXX JavaScr ...

  8. 由ASP.NET所谓前台调用后台、后台调用前台想到HTTP

    由ASP.NET所谓前台调用后台.后台调用前台想到HTTP 在由ASP.NET所谓前台调用后台.后台调用前台想到HTTP——理论篇中描述了一下ASP.NET新手的三个问题及相关的HTTP协议内容,在由 ...

  9. ASP.NET所谓前台调用后台、后台调用前台想到HTTP——实践篇

    由ASP.NET所谓前台调用后台.后台调用前台想到HTTP——实践篇 在由ASP.NET所谓前台调用后台.后台调用前台想到HTTP——理论篇中描述了一下ASP.NET新手的三个问题及相关的HTTP协议 ...

随机推荐

  1. 关于Django的Ajax操作

    一 什么是Ajax AJAX(Asynchronous Javascript And XML)翻译成中文就是"异步Javascript和XML".即使用Javascript语言与服 ...

  2. window下的Django入门

    一.window下新建安装(参考书籍:<python编程:从入门到实践>) 新建一个文件夹 learning_log ,在终端中切换到该目录下,并创建一个虚拟工作环境,运行模块 venv  ...

  3. SPOJ 839 Optimal Marks(最小割的应用)

    https://vjudge.net/problem/SPOJ-OPTM 题意: 给出一个无向图G,每个点 v 以一个有界非负整数 lv 作为标号,每条边e=(u,v)的权w定义为该边的两个端点的标号 ...

  4. Js 运行机制 (重点!!)

    一.引子 本文介绍JavaScript运行机制,这一部分比较抽象,我们先从一道面试题入手: 这一题看似很简单,但如果你不了解JavaScript运行机制,很容易就答错了.题目的答案是依次输出1 2 3 ...

  5. css的postion属性

    在实际项目中,发现postion这个属性经常使用而且常常很重要,所以总结整理一下知识点 css中postion属性有以下可选值,分别是:static,absolute, fixed, relative ...

  6. npm和yarn的区别

    npm和yarn的区别,我们该如何选择? 周一入职,同事JJ让我熟悉一下基于React的新项目.按照以往,我的步骤都是: git clone xxx npm install npm run dev 这 ...

  7. maven配置环境变量失败解决办法

    配置maven路径什么的统统正确,最后测hi不成功.在网上搜索了好多资料方法都解不了 具体问题具体对待吧,如果有和我类似的小伙伴,可以尝试一下我的这个办法,在maven路径后面加/bin path变量 ...

  8. 第 3 章 镜像 - 020 - 搭建本地 Registry

    Docker Hub 虽然非常方便,但还是有些限制,比如: 需要 internet 连接,而且下载和上传速度慢. 上传到 Docker Hub 的镜像任何人都能够访问,虽然可以用私有 reposito ...

  9. Skip level 1 on 1

    2019-01-08 16:43:29 Skip level 1:1 什么是 Skip level 1 on  1 :你和你老板的老板(的老板) 1:1 如果你的老板是first line manag ...

  10. docker 安装完mysql 后客户端无法访问

    1.在虚拟机的centos 中安装 docker 的mysql 镜像. docker run --name mysql01 -p 3306:3306 -e MYSQL_ROOT_PASSWORD=12 ...