HttpClient 5.0 get/post简单封装

  • by

HttpClient 5.0.x:http://hc.apache.org/httpcomponents-client-5.0.x

此前使用网络上找的一些HttpUtils进行使用,效果和性能都不是特别理想,所以使用HttpClient自己进行了简单的封装。

package utils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.entity.UrlEncodedFormEntity;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.message.BasicNameValuePair;

public class HttpUtils {
  public static final int HTTP_METHOD_GET = 0;
  public static final int HTTP_METHOD_POST = 1;
  
  public static Object call(int method, String url) {
    switch(method) {
      case HTTP_METHOD_GET:
        return HttpUtils.doGet(url, new HashMap<String, String>(), "", new HashMap<String, String>());
      case HTTP_METHOD_POST:
        return HttpUtils.doPost(url, new HashMap<String, String>(), "", new HashMap<String, String>());
      default:
        return false;
    }
  }
  
  public static Object call(int method, String url, HashMap<String, String> params) {
    switch(method) {
      case HTTP_METHOD_GET:
        return HttpUtils.doGet(url, params, "", new HashMap<String, String>());
      case HTTP_METHOD_POST:
        return HttpUtils.doPost(url, params, "", new HashMap<String, String>());
      default:
        return false;
    }
  }
  
  public static Object call(int method, String url, HashMap<String, String> params, HashMap<String, String> headers) {
    switch(method) {
      case HTTP_METHOD_GET:
        return HttpUtils.doGet(url, params, "", headers);
      case HTTP_METHOD_POST:
        return HttpUtils.doPost(url, params, "", headers);
      default:
        return false;
    }
  }
  
  public static Object call(int method, String url, HashMap<String, String> params, HashMap<String, String> headers, String cookies) {
    switch(method) {
      case HTTP_METHOD_GET:
        return HttpUtils.doGet(url, params, cookies, headers);
      case HTTP_METHOD_POST:
        return HttpUtils.doPost(url, params, cookies, headers);
      default:
        return false;
    }
  }
  
  private static Object doGet(String url, HashMap<String, String> params, String cookies, HashMap<String, String> headers) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    StringBuilder getParams = new StringBuilder();
    params.forEach((k, v) -> {
      getParams.append((getParams.length() == 0 ? "?" : "&") + k + "=" + v);
    });
    
    HttpGet obj = new HttpGet(url + getParams);
    obj.setHeader("cookie", cookies);
    headers.forEach((k, v) -> {
      obj.setHeader(k, v);
    });
    
    try {
      CloseableHttpResponse resp = httpClient.execute(obj);
      
      try {
        return EntityUtils.toString(resp.getEntity());
      } catch (ParseException e) {
        System.out.println(e);
        return false;
      }
    } catch (IOException e) {
      System.out.println(e);
      return false;
    }
  }
  
  private static Object doPost(String url, HashMap<String, String> params, String cookies, HashMap<String, String> headers) {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost obj = new HttpPost(url);
    obj.setHeader("cookie", cookies);
    headers.forEach((k, v) -> {
      obj.setHeader(k, v);
    });
    List <NameValuePair> postParams = new ArrayList <NameValuePair>();
    params.forEach((k, v) -> {
      postParams.add(new BasicNameValuePair(k, v));
    });
    obj.setEntity(new UrlEncodedFormEntity(postParams));
    try {
      CloseableHttpResponse resp = httpClient.execute(obj);
      
      try {
        return EntityUtils.toString(resp.getEntity());
      } catch (ParseException e) {
        System.out.println(e);
        return false;
      }
    } catch (IOException e) {
      System.out.println(e);
      return false;
    }
  }
}

调用方法:

HashMap<String, String> params = new HashMap<String, String>();
params.put("params", "value");
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("User-Agent", "Samler Test/1.0");
System.out.println(HttpUtils.call(HttpUtils.HTTP_METHOD_GET, "http://www.example.com/", params, "", headers));