自己封装的调用HttpClient的HttpServiceCaller类

package xxx.com.util;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpMethodBase;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.io.IOUtils;

/**
 * @ClassName: HttpServiceCaller.java
 * @Description: TODO
 * @author SonicWu
 * @date 2009-11-4 下午06:42:56
 */
public class HttpServiceCaller {

    /**
     * @param requestURL
     * @param data
     * @return responseText
     * @throws Exception
     */
    public static String postMethod(String requestURL, NameValuePair[] data) throws Exception {
        PostMethod postMethod = new PostMethod(requestURL);
        postMethod.setRequestBody(data);

        return call(requestURL, postMethod);
    }

    /**
     * @param requestURL
     * @param data
     * @return responseText
     * @throws Exception
     */
    public static String postUTF8Method(String requestURL, NameValuePair[] data) throws Exception {
        PostMethod postMethod = new PostMethod(requestURL);
        postMethod.setRequestBody(data);
        postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
        postMethod.addRequestHeader("Connection", "close");
        return call(requestURL, postMethod);
    }

    /**
     * @author jackzhou
     * @param requestURL
     * @param data
     * @return
     * @throws Exception
     */
    public static String postMethod(String requestURL, HashMap<String, String> postDate) throws Exception {

        NameValuePair[] data = new NameValuePair[postDate.entrySet().size()];
        int i=0;
        for(Map.Entry<String, String> entry : postDate.entrySet()) {
            data[i] = new NameValuePair(entry.getKey(), entry.getValue());
            i++;
        }

        PostMethod postMethod = new PostMethod(requestURL);
        postMethod.setRequestBody(data);
        postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

        HttpClient httpClient = new HttpClient();
        String responseText = null;

        int statusCode = httpClient.executeMethod(postMethod);

        if (statusCode != 200) {
            String errorMessage = IOUtils.toString(postMethod.getResponseBodyAsStream(), postMethod.getResponseCharSet());
            throw new Exception("call failed : " + errorMessage);
        }
        responseText = IOUtils.toString(postMethod.getResponseBodyAsStream(), postMethod.getResponseCharSet());

        return responseText;
    }

public static String postMethod(String requestURL, HashMap<String, String> postDate, String encode) throws Exception {
        NameValuePair[] data = new NameValuePair[postDate.entrySet().size()];
        int i=0;
        for(Map.Entry<String, String> entry : postDate.entrySet()) {
            data[i] = new NameValuePair(entry.getKey(), entry.getValue());
            i++;
        }

        PostMethod postMethod = new PostMethod(requestURL);
        postMethod.setRequestBody(data);
        postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");

        HttpClient httpClient = new HttpClient();
        String responseText = null;

        int statusCode = httpClient.executeMethod(postMethod);

        if (statusCode != 200) {
            String errorMessage = IOUtils.toString(postMethod.getResponseBodyAsStream(), encode);
            throw new Exception("call failed : " + errorMessage);
        }
        responseText = IOUtils.toString(postMethod.getResponseBodyAsStream(), encode);

        return responseText;
    }

    /**
     * @param requestURL
     * @return responseText
     * @throws Exception
     */
    public static String getMethod(String requestURL) throws Exception {
        GetMethod getMethod = new GetMethod(requestURL);

        return call(requestURL, getMethod);
    }

    /**
     * @param requestURL
     * @param method
     * @return responseText
     * @throws Exception
     */
    private static String call(String requestURL, HttpMethod method) throws Exception {
        HttpClient httpClient = new HttpClient();

        //会报错 HTTP 503 special method ....
//        method.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
//        method.addRequestHeader("Connection", "close");

        String responseText = null;
        try {
            int statusCode = httpClient.executeMethod(method);

            if (statusCode != 200) {
                String errorMessage = method.getResponseBodyAsString();
                throw new Exception("call failed : " + errorMessage);
            }


            StringBuilder responseBody = new StringBuilder();
            InputStream is = method.getResponseBodyAsStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));

            String line;
            while((line = br.readLine())!=null) {
                responseBody.append(line);
            }

            responseText = responseBody.toString();

            br.close();
            is.close();
        } catch (Exception e) {
            throw e;
        } finally {
            SimpleHttpConnectionManager connectionManager = (SimpleHttpConnectionManager) httpClient.getHttpConnectionManager();
            connectionManager.shutdown();

            method.releaseConnection();
        }

        return responseText;
    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值