美文网首页
HTTP 请求 HttpClientUtils

HTTP 请求 HttpClientUtils

作者: 忘记_3a6a | 来源:发表于2020-03-09 10:02 被阅读0次
  • http请求工具类

package com.qing;


import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
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 sun.misc.BASE64Encoder;

import java.io.IOException;

public class HttpClientUtils {

    public static final String GET="get";

    public static final String POST="post";

    public static final String REQUEST_HEADER_CONNECTION="keep-alive";

    public static final String REQUEST_HEADER_USER_AGENT="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36";

    public static  String doGet(String url){
        return  requestMethod(url,GET,null,null);
    }

    public static  String doGet(String url,String jsonParams){
        return  requestMethod(url,GET,null,jsonParams);
    }

    public static  String doGet(String url,String jsonParams,String cookie){
        return  requestMethod(url,GET,cookie,jsonParams);
    }



    public static String doPost(String url){
        return requestMethod(url,POST,null,null);
    }


    public static String doPost(String url,String jsonParams){
        return requestMethod(url,POST,null,jsonParams);
    }


    public static String doPost(String url,String cookie,String jsonParams){
        return requestMethod(url,POST,cookie,jsonParams);
    }






    private static String requestMethod(String url,String requestMethod,String cookie,String jsonParams){
        String result=null;
        CloseableHttpClient httpClient= null;
        //发送请求
        CloseableHttpResponse httpResponse=null;
        try {
            //创建客户端,打开浏览器
            httpClient = HttpClients.createDefault();
            HttpGet httpGet=null;
            HttpPost httpPost=null;

            if(GET.equals(requestMethod)){
                 httpGet=new HttpGet(url);
                //模拟浏览器
                httpGet.setHeader("User-Agent", REQUEST_HEADER_USER_AGENT);
                //长链接
                httpGet.setHeader("Connection",REQUEST_HEADER_CONNECTION);

                if (StringUtils.isNotEmpty(cookie)) {
                    httpGet.setHeader("cookie",cookie);
                }
                if(StringUtils.isNotBlank(jsonParams)){
                    HttpEntity httpEntity=new StringEntity(jsonParams,"utf-8");
                    httpPost.setEntity(httpEntity);
                }
                httpResponse=httpClient.execute(httpGet);

            }
            else if(POST.equals(requestMethod)){
                httpPost=new HttpPost(url);
               //模拟浏览器
                httpPost.setHeader("User-Agent", REQUEST_HEADER_USER_AGENT);
                //长链接
                httpPost.setHeader("Connection",REQUEST_HEADER_CONNECTION);
                httpPost.setHeader("Content-Type","application/json;charset=utf8");

                if (StringUtils.isNotEmpty(cookie)) {
                    httpPost.setHeader("cookie",cookie);
                }

                //有参数
                if(StringUtils.isNotBlank(jsonParams)){
                    HttpEntity httpEntity=new StringEntity(jsonParams,"utf-8");
                    httpPost.setEntity(httpEntity);
                }
                httpResponse=httpClient.execute(httpPost);
            }
            HttpEntity httpEntity= httpResponse.getEntity();
            result=EntityUtils.toString(httpEntity,"utf-8");

            //获取图片
         /*   byte[] data=EntityUtils.toByteArray(httpEntity);
            //对字节数组Base64编码
            BASE64Encoder encoder = new BASE64Encoder();
            //返回Base64编码过的字节数组字符串
            result=encoder.encode(data);*/

        } catch (ParseException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if(httpResponse!=null){
                    httpResponse.close();
                }
                if(httpClient!=null){
                    httpClient.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return result;
    }



}

  • 引入依赖包
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

<!-- Apache Http Begin -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>fluent-hc</artifactId>
    <version>4.5.5</version>
</dependency>
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpmime</artifactId>
    <version>4.5.5</version>
</dependency>
<!-- Apache Http End -->

<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>   




相关文章

网友评论

      本文标题:HTTP 请求 HttpClientUtils

      本文链接:https://www.haomeiwen.com/subject/exhudhtx.html