java Source

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            14 Posts :: 24 Stories :: 8 Comments :: 0 Trackbacks
          package org.lambdasoft.http;

          import java.util.Map;

          /**
           * 
          @author lei.tang (justinlei@gmail.com)
           * @date 
           * 
          @version
           
          */
          public interface HttpRequest {
              String execute(String url,Map
          <String, String> params) throws Exception;
          }

          package org.lambdasoft.http;

          import java.util.Map;
          import java.util.Set;

          import org.apache.commons.httpclient.HttpClient;
          import org.apache.commons.httpclient.HttpStatus;
          import org.apache.commons.httpclient.NameValuePair;
          import org.apache.commons.httpclient.methods.GetMethod;

          /**
           * 
          @author lei.tang (justinlei@gmail.com)
           * @date 
           * 
          @version
           
          */
          public class GetRequest implements HttpRequest{

              @Override
              
          public String execute(String url, Map<String, String> params) throws Exception {
                  HttpClient client 
          = new HttpClient();
                  GetMethod getMethod 
          = new GetMethod(url);
                  
          if(params != null && params.size() != 0) {
                      Set
          <String> keySet = params.keySet();
                      NameValuePair[] nameValuePairs 
          = new NameValuePair[params.size()];
                      
          int i = 0;
                      
          for (String key : keySet) {
                          nameValuePairs[i] 
          = new NameValuePair(key, params.get(key));
                          i
          ++;
                      }
                      getMethod.setQueryString(nameValuePairs);
                  }
                  
          int statusCode = client.executeMethod(getMethod);
                  
          if (statusCode != HttpStatus.SC_OK) {
                      
          throw new Exception(getMethod.getStatusLine().toString());
                  }
                  
          return new String(getMethod.getResponseBody());
              }
          }




          package org.lambdasoft.http;

          import java.util.HashMap;
          import java.util.Map;
          import java.util.Set;

          import org.apache.commons.httpclient.HttpClient;
          import org.apache.commons.httpclient.HttpStatus;
          import org.apache.commons.httpclient.NameValuePair;
          import org.apache.commons.httpclient.methods.PostMethod;

          /**
           * 
          @author lei.tang (justinlei@gmail.com)
           * @date 
           * 
          @version
           
          */
          public class PostRequest implements HttpRequest{

              @Override
              
          public String execute(String url, Map<String, String> params)
                      
          throws Exception {
                  HttpClient client 
          = new HttpClient();
                  PostMethod postMethod 
          = new PostMethod(url);
                  
                  
          if(params != null && params.size() != 0) {
                      Set
          <String> keySet = params.keySet();
                      NameValuePair[] nameValuePairs 
          = new NameValuePair[params.size()];
                      
          int i = 0;
                      
          for (String key : keySet) {
                          nameValuePairs[i] 
          = new NameValuePair(key, params.get(key));
                          i
          ++;
                      }
                      postMethod.setQueryString(nameValuePairs);
                  }
                  
          int statusCode = client.executeMethod(postMethod);
                  
          if (statusCode != HttpStatus.SC_OK) {
                      
          throw new Exception(postMethod.getStatusLine().toString());
                  }
                  
          return new String(postMethod.getResponseBody());
              }

          }
          posted on 2010-04-06 16:29 JustinLei 閱讀(2263) 評論(2)  編輯  收藏

          Feedback

          # re: apache commons httpclient[未登錄] 2010-04-06 20:55 dd
          你這是httpclient 3.0的代碼,這個版本有很多制限,還是不要用這個版本,有誤導人的嫌疑 。  回復  更多評論
            

          # re: apache commons httpclient 2010-04-07 00:26 JustinLei
          詳細?  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 连云港市| 准格尔旗| 博湖县| 黄平县| 和田县| 宁阳县| 阳江市| 南昌县| 岫岩| 双流县| 图片| 喜德县| 朝阳市| 广宁县| 宁强县| 南部县| 南平市| 澄江县| 玉林市| 修文县| 绿春县| 南部县| 开平市| 泗阳县| 曲阜市| 九江县| 定南县| 伊春市| 类乌齐县| 镇雄县| 齐河县| 阿鲁科尔沁旗| 故城县| 南川市| 上杭县| 楚雄市| 丹东市| 郧西县| 荔波县| 东乌珠穆沁旗| 磐安县|