隨筆-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.aygfsteel.com/bolo
          posted on 2013-11-05 16:20 菠蘿大象 閱讀(2948) 評論(0)  編輯  收藏 所屬分類: Java
          主站蜘蛛池模板: 汉源县| 陈巴尔虎旗| 甘泉县| 兴国县| 揭东县| 卢湾区| 左权县| 都兰县| 渑池县| 潜江市| 广西| 麻江县| 张掖市| 萨嘎县| 明水县| 河北省| 巧家县| 利津县| 阜南县| 三门峡市| 孝昌县| 滨海县| 临潭县| 宿松县| 延安市| 邓州市| 福海县| 兰西县| 保山市| 朔州市| 宁乡县| 酒泉市| 兴化市| 肥东县| 蒲江县| 革吉县| 陕西省| 琼结县| 永仁县| 元江| 囊谦县|