commons-httpclient 服务端模拟客户端发送请求

### 纯属当个笔记记录

package com.roach.springmvc.utils;

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

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.DeleteMethod;
import org.apache.commons.httpclient.methods.EntityEnclosingMethod;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.PutMethod;
import org.apache.commons.httpclient.methods.StringRequestEntity;

/**
 * 服务端模拟客户端发送请求工具
 * 
 * @author roach
 *
 */
public class HttpClientUtils {

    private static final String UTF_8 = "UTF-8";

    /**
     * POST 请求
     * 
     * @param requestUrl
     *            请求地址
     * @param params
     *            请求参数
     */
    public static String httpPostRequest(String requestUrl, String params) {
        PostMethod post = new PostMethod(requestUrl);
        return returnResult(post, params, UTF_8);
    }

    /**
     * POST 请求
     * 
     * @param requestUrl
     *            请求地址
     * @param params
     *            请求参数
     * @param charset
     *            字符集
     */
    public static String httpPostRequest(String requestUrl, String params, String charset) {
        PostMethod post = new PostMethod(requestUrl);
        return returnResult(post, params, charset);
    }

    /**
     * GET 方法
     * 
     * @param requestUrl
     * @return
     */
    public static String httpGetRequest(String requestUrl) {
        return httpGetRequest(requestUrl, UTF_8);
    }

    /**
     * GET 方法
     * 
     * @param requestUrl
     * @return
     */
    public static String httpGetRequest(String requestUrl, String charset) {
        GetMethod get = new GetMethod(requestUrl);
        return returnResult(get, charset);
    }

    /**
     * PUT 请求
     * 
     * @param requestUrl
     *            请求地址
     * @param params
     *            请求参数
     */
    public static String httpPutRequest(String requestUrl, String params) {
        return httpPutRequest(requestUrl, params, UTF_8);
    }

    /**
     * PUT 请求
     * 
     * @param requestUrl
     *            请求地址
     * @param params
     *            请求参数
     * @param charset
     *            字符集
     */
    public static String httpPutRequest(String requestUrl, String params, String charset) {
        PutMethod put = new PutMethod(requestUrl);
        return returnResult(put, params, charset);
    }

    /**
     * Delete 方法
     * 
     * @param requestUrl
     * @return
     */
    public static String httpDeleteRequest(String requestUrl) {
        return httpDeleteRequest(requestUrl, UTF_8);
    }

    /**
     * Delete 方法
     * 
     * @param requestUrl
     * @return
     */
    public static String httpDeleteRequest(String requestUrl, String charset) {
        DeleteMethod delete = new DeleteMethod(requestUrl);
        return returnResult(delete, charset);
    }

    /**
     * 抽离封装POST 和 PUT 请求的公共代码
     * 
     * @param requestMethod
     * @param params
     * @param charset
     * @return
     */
    private static String returnResult(EntityEnclosingMethod requestMethod, String params, String charset) {

        String result = null;
        try {
            requestMethod.setRequestHeader("Content-Type", "application/json");
            if (params != null) {
                requestMethod.setRequestEntity(new StringRequestEntity(params, "application/json", charset));
            }

            HttpClient client = new HttpClient();
            int statusCode = client.executeMethod(requestMethod);
            if (statusCode != HttpStatus.SC_OK) {
                return null;
            }
            byte[] bytes = readInputStream(requestMethod.getResponseBodyAsStream());
            result = new String(bytes, charset);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            requestMethod.releaseConnection();
        }
        return result;
    }

    /**
     * 抽离封装出GET, DELETE公共代码
     * 
     * @param requestMethod
     * @param charset
     * @return
     */
    private static String returnResult(HttpMethodBase requestMethod, String charset) {

        requestMethod.setRequestHeader("Content-Type", "application/json");

        String result = null;
        try {
            HttpClient client = new HttpClient();
            int statusCode = client.executeMethod(requestMethod);
            if (statusCode != HttpStatus.SC_OK) {
                return null;
            }
            byte[] bytes = readInputStream(requestMethod.getResponseBodyAsStream());
            result = new String(bytes, charset);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            requestMethod.releaseConnection();
        }
        return result;
    }

    private static byte[] readInputStream(InputStream in) throws IOException {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        byte[] b = new byte[1024];
        int len = 0;
        while ((len = in.read(b)) != -1) {
            out.write(b, 0, len);
        }
        byte[] array = out.toByteArray();

        out.flush();
        out.close();
        in.close();
        return array;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值