随笔-67  评论-522  文章-0  trackbacks-0
    存档一份使用commons-httpclient发送请求的代码片断,为了大象越来越衰弱的记忆力,像这种不经常使用,但在某个需要的时刻又想不起来的情况。
    代码里的commons-httpclient3.1版本,没有任何技术含量,所以代码也没写注释,放在博客上面是为了大象方便查找^_^,各位亲直接无视吧。
import java.io.IOException;
import java.util.Map;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.lang.StringUtils;

/**
 * 使用HttpClient发送请求
 * 
@author 菠萝大象
 
*/
public class HttpRequestClient {
    
    public static String postRequest(String url, byte[] postData, String contentType) {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        ByteArrayRequestEntity bare = new ByteArrayRequestEntity(postData);
        postMethod.setRequestHeader("Connection", "close");
        postMethod.addRequestHeader("Content-Type",contentType+";charset=UTF-8");
        postMethod.setRequestHeader("Content-Length", String.valueOf(bare.getContentLength()));
        postMethod.setRequestEntity(new ByteArrayRequestEntity(postData));
        httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
        String result = StringUtils.EMPTY;
        try {
            httpClient.executeMethod(postMethod);
            result = postMethod.getResponseBodyAsString();
            postMethod.releaseConnection();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            postMethod.releaseConnection();
            if (httpClient != null) {
                ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
                httpClient = null;
            }
        }
        return result;
    }
    
    public static String postRequest(String url, Map<String, String> params, String contentType) {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
        postMethod.setRequestHeader("Connection", "close");
        postMethod.addRequestHeader("Content-Type",contentType+";charset=UTF-8");
        for(Map.Entry<String, String> entry : params.entrySet()){
            postMethod.addParameter(entry.getKey(), entry.getValue());
        }
        httpClient.getParams().setBooleanParameter("http.protocol.expect-continue", false);
        String result = StringUtils.EMPTY;
        try {
            httpClient.executeMethod(postMethod);
            result = postMethod.getResponseBodyAsString();
            postMethod.releaseConnection();
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            postMethod.releaseConnection();
            if (httpClient != null) {
                ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
                httpClient = null;
            }
        }
        return result;
    }
}
    本文为菠萝大象原创,如要转载请注明出处。http://www.blogjava.net/bolo
posted on 2013-11-05 16:20 菠萝大象 阅读(2930) 评论(0)  编辑  收藏 所属分类: Java

只有注册用户登录后才能发表评论。


网站导航: