<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆-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.tkk7.com/bolo
    posted on 2013-11-05 16:20 菠蘿大象 閱讀(2944) 評論(0)  編輯  收藏 所屬分類: Java
    主站蜘蛛池模板: 三年片在线观看免费观看大全中国| 在线观看亚洲AV日韩A∨| 国产区在线免费观看| 亚洲AV无码成H人在线观看| 亚洲国产精品无码久久98| 免费爱爱的视频太爽了| 亚洲av无码兔费综合| 又大又粗又爽a级毛片免费看| 美国免费高清一级毛片| 亚洲中文字幕无码爆乳av中文| a在线视频免费观看在线视频三区| 国产精品V亚洲精品V日韩精品| 香蕉免费看一区二区三区| 亚洲成a人片77777kkkk| 一级毛片成人免费看免费不卡| 精品亚洲成AV人在线观看| 免费av欧美国产在钱| 国产成人精品亚洲| 亚洲一区AV无码少妇电影☆| 日韩精品内射视频免费观看| 亚洲一卡二卡三卡| 国产三级免费电影| a级毛片在线免费观看| 亚洲日韩乱码中文无码蜜桃臀| 两个人的视频高清在线观看免费 | 最近中文字幕免费mv视频8| 亚洲老熟女五十路老熟女bbw| 亚洲AⅤ优女AV综合久久久| 国产一级片免费看| 亚洲AV色吊丝无码| 亚洲av日韩av欧v在线天堂| 久久成人a毛片免费观看网站| 91丁香亚洲综合社区| 亚洲国产精品成人久久蜜臀| 久久精品无码精品免费专区| 亚洲已满18点击进入在线观看| 亚洲视频在线精品| 2021国产精品成人免费视频| 一级做a爰片久久毛片免费陪| 亚洲电影一区二区三区| 国产精品免费看久久久久|