HttpClient工具类

加载maven依赖

  <dependency>
      <groupId>commons-httpclient</groupId>
      <artifactId>commons-httpclient</artifactId>
      <version>3.1</version>
  </dependency>

工具类

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;

/**
 * HTTP请求工具
 * @author zhangxj
 *
 */
public class HttpClient{

    public static String is2Str(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i = -1;
        while ((i = is.read()) != -1) {
            baos.write(i);
        }
        return baos.toString();
    }

    public static String doPosts(String url, NameValuePair[] nameValuePairs) {

        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        if (nameValuePairs != null) {
            post.setRequestBody(nameValuePairs);
        }

        try {
            client.executeMethod(post);
            return is2Str(post.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            post.releaseConnection();
        }
        return null;
    }

    public static Object doPosts(String url) throws UnsupportedEncodingException {

        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        post.setRequestHeader("Content-Type", "application/json");

        try {
            client.executeMethod(post);
            return is2Str(post.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            post.releaseConnection();
        }
        return null;
    }


    public static String doGet(String url, String token) {

        HttpClient client = new HttpClient();
        GetMethod get = new GetMethod(url);
        if (token != null) {
            get.setRequestHeader("Authorization", "Bearer " + token);
        }

        try {
            client.executeMethod(get);
            return is2Str(get.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            get.releaseConnection();
        }
        return null;
    }

    @SuppressWarnings("deprecation")
    public static int doPosts(String url, String params, String token) throws UnsupportedEncodingException {

        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        post.setRequestHeader("Content-Type", "application/json");
        if (token != null) {
            post.setRequestHeader("Authorization", "Bearer " + token);
        }
        if (params != null) {
            //post.setRequestBody(params);
            InputStream in = new ByteArrayInputStream(params.getBytes("utf-8"));
            post.setRequestBody(in);
        }

        try {
            client.executeMethod(post);
            return post.getStatusCode();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            post.releaseConnection();
        }
        return 0;
    }

    public static int doDelete(String url, String token) {

        HttpClient client = new HttpClient();
        DeleteMethod delete = new DeleteMethod(url);
        if (token != null) {
            delete.setRequestHeader("Authorization", "Bearer " + token);
        }

        try {
            client.executeMethod(delete);
            return delete.getStatusCode();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            delete.releaseConnection();
        }
        return 0;
    }

    @SuppressWarnings("deprecation")
    public static int doPuts(String url, String params, String token) throws UnsupportedEncodingException {

        HttpClient client = new HttpClient();
        PutMethod put = new PutMethod(url);
        if (token != null) {
            put.setRequestHeader("Authorization", "Bearer " + token);
        }
        if (params != null) {
            // put.setRequestBody(params);
            InputStream in = new ByteArrayInputStream(params.getBytes("utf-8"));
            put.setRequestBody(in);
        }

        try {
            client.executeMethod(put);
            return put.getStatusCode();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            put.releaseConnection();
        }
        return 0;
    }

    public static String doPost(String url, NameValuePair[] nameValuePairs) {
        HttpClient client = new HttpClient();
        PostMethod post = new PostMethod(url);
        if (nameValuePairs != null) {
            post.setRequestBody(nameValuePairs);
        }

        try {
            client.executeMethod(post);
            return is2Str(post.getResponseBodyAsStream());
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            post.releaseConnection();
        }
        return null;
    }

}
上一篇:Centos8系统中du命令详解


下一篇:C# 应用 - 使用 HttpClient 发起上传文件、下载文件请求