2021-01-27-Httpclient工具类

作用

  1. 后端向后端发请求
  2. 可以让后端跨内外网
    比如:一个前端项目a,一个后端项目b,一个后端项目c
    a,b,c都处于内网,现在把a,b做一个外网代理,c不做
    此时a,b都已经不能访问c了,然而,如果b用httpcliet发请求,它就可以访问到c了

pom.xml

  <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.6</version>
        </dependency>
        <!-- JSONObject对象依赖的jar包 开始 -->
        <dependency>
            <groupId>commons-beanutils</groupId>
            <artifactId>commons-beanutils</artifactId>
            <version>1.9.3</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>commons-logging</groupId>
            <artifactId>commons-logging</artifactId>
            <version>1.1.1</version>
        </dependency>
        <dependency>
            <groupId>net.sf.ezmorph</groupId>
            <artifactId>ezmorph</artifactId>
            <version>1.0.6</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.2.3</version>
            <classifier>jdk15</classifier>
            <!-- jdk版本 -->
        </dependency>

        <!-- Json依赖架包下载结束 -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

工具类代码

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Component
public class HttpUtils {

    public String doGetJson(String url, String token) throws Exception, IOException {
        //初始化httpClient
        DefaultHttpClient client = new DefaultHttpClient();
        //用Get方式进行提交
        HttpGet httpGet = new HttpGet(url);
        httpGet.setHeader("X-AUTH-TOKEN", token);

        //发送请求
        HttpResponse response = client.execute(httpGet);
        //获取数据
        HttpEntity entity = response.getEntity();
        //格式转换
        String result = null;
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        //释放链接
        httpGet.releaseConnection();
        return result;
    }

    public JSONObject doGetJson(String url) throws Exception, IOException {
        JSONObject jsonObject=null;
        //初始化httpClient
        DefaultHttpClient client = new DefaultHttpClient();
        //用Get方式进行提交
        HttpGet httpGet = new HttpGet(url);
        //发送请求
        HttpResponse response = client.execute(httpGet);
        //获取数据
        HttpEntity entity = response.getEntity();
        //格式转换
        String result = null;
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
            jsonObject=JSONObject.fromObject(result);
        }
        //释放链接
        httpGet.releaseConnection();
        return jsonObject;
    }

    public String doPostJson(String url) throws Exception, IOException {
        //初始化httpClient
        DefaultHttpClient client = new DefaultHttpClient();
        //用Get方式进行提交
        HttpPost httpPost = new HttpPost(url);
        //发送请求
        HttpResponse response = client.execute(httpPost);
        //获取数据
        HttpEntity entity = response.getEntity();
        //格式转换
        String result = null;
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        //释放链接
        httpPost.releaseConnection();
        return result;
    }

    public String doPostJson(String url, String token) throws Exception, IOException {
        //初始化httpClient
        DefaultHttpClient client = new DefaultHttpClient();
        //用Get方式进行提交
        HttpPost httpPost = new HttpPost(url);
        httpPost.setHeader("X-AUTH-TOKEN", token);

        //发送请求
        HttpResponse response = client.execute(httpPost);
        //获取数据
        HttpEntity entity = response.getEntity();
        //格式转换
        String result = null;
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        //释放链接
        httpPost.releaseConnection();
        return result;
    }
    public String doPostJson(String url,JSONObject param, String token) throws Exception, IOException {
        //初始化httpClient
        DefaultHttpClient client = new DefaultHttpClient();
        //用Get方式进行提交
        StringEntity stringEntity=new StringEntity(param.toString(), StandardCharsets.UTF_8);
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(stringEntity);
        httpPost.setHeader("X-AUTH-TOKEN", token);
        //设置请求头
        httpPost.setHeader("Content-Type","application/json");
        httpPost.setHeader("Charset","UTF-8");


        //发送请求
        HttpResponse response = client.execute(httpPost);
        //获取数据
        HttpEntity entity = response.getEntity();
        //格式转换
        String result = null;
        if (entity != null) {
            result = EntityUtils.toString(entity, "UTF-8");
        }
        //释放链接
        httpPost.releaseConnection();
        return result;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值