J2EE工具類:WebHttpClient.java

  1. import java.io.BufferedReader;   
  2. import java.io.IOException;   
  3. import java.io.InputStreamReader;   
  4. import java.util.HashMap;   
  5. import java.util.Iterator;   
  6. import java.util.Map;   
  7. import java.util.Set;   
  8.   
  9. import org.apache.commons.httpclient.HttpClient;   
  10. import org.apache.commons.httpclient.HttpException;   
  11. import org.apache.commons.httpclient.HttpStatus;   
  12. import org.apache.commons.httpclient.NameValuePair;   
  13. import org.apache.commons.httpclient.methods.GetMethod;   
  14. import org.apache.commons.httpclient.methods.PostMethod;   
  15.   
  16. public class WebHttpClient {   
  17.   
  18.     /**
  19.       * 獲得網頁中的所有HTML內容
  20.       * @param url
  21.       * @param charset
  22.       * @return
  23.       */  
  24.     public String getWebContentByGet(String url,String charset){   
  25.          HttpClient client = new HttpClient();   
  26.          GetMethod getMethod = new GetMethod(url);   
  27.          StringBuilder sb = new StringBuilder();   
  28.         try {   
  29.             // 狀態碼   
  30.             int statusCode=client.executeMethod(getMethod);   
  31.             if (statusCode == HttpStatus.SC_OK) {   
  32.                 //獲得HTML文本   
  33.                  BufferedReader bf = new BufferedReader(new InputStreamReader(   
  34.                          getMethod.getResponseBodyAsStream(), charset));   
  35.                  String line = null;   
  36.                 while ((line = bf.readLine()) != null) {   
  37.                      sb.append(line).append("/r/n");   
  38.                  }   
  39.                  bf.close();   
  40.              }   
  41.          } catch (Exception e) {   
  42.              e.printStackTrace();   
  43.          } finally {   
  44.              getMethod.releaseConnection();   
  45.          }   
  46.         return sb.toString();   
  47.      }   
  48.     /**
  49.       * 獲得網頁中的所有HTML內容
  50.       * @param url
  51.       * @param mapData:傳遞的參數
  52.       * @param charset
  53.       * @return
  54.       */  
  55.     public String getWebContentByPost(String url,Map<String,String> mapData,String charset){   
  56.          HttpClient client = new HttpClient();   
  57.          PostMethod postMethod = new PostMethod(url);   
  58.          StringBuilder sb = new StringBuilder();   
  59.         // 填入各個表單域的值   
  60.          NameValuePair[] data = new NameValuePair[mapData.size()];   
  61.          Set set = mapData.entrySet();   
  62.          Iterator iterator = set.iterator();   
  63.         int i=0;   
  64.         while (iterator.hasNext()) {   
  65.              Map.Entry entry = (Map.Entry) iterator.next();   
  66.              data[i]=new NameValuePair((String)entry.getKey(),(String)entry.getValue());   
  67.              i++;   
  68.          }   
  69.         /*
  70.          NameValuePair[] data = {new NameValuePair("toPath","toPath"),
  71.                                  new NameValuePair("action","login"),
  72.                                  new NameValuePair("loginname","13761083826"),
  73.                                  new NameValuePair("password","111111")
  74.                                  };
  75.          */  
  76.         // 將表單的值放入postMethod中   
  77.          postMethod.setRequestBody(data);   
  78.         try {   
  79.             int statusCode = client.executeMethod(postMethod);   
  80.             if (statusCode == HttpStatus.SC_OK) {   
  81.                 //獲得HTML文本   
  82.                  BufferedReader bf = new BufferedReader(new InputStreamReader(   
  83.                          postMethod.getResponseBodyAsStream(), charset));   
  84.                  String line = null;   
  85.                 while ((line = bf.readLine()) != null) {   
  86.                      sb.append(line).append("/r/n");   
  87.                  }   
  88.                  bf.close();   
  89.              }   
  90.          } catch (HttpException e) {   
  91.             // TODO Auto-generated catch block   
  92.              e.printStackTrace();   
  93.          } catch (IOException e) {   
  94.             // TODO Auto-generated catch block   
  95.              e.printStackTrace();   
  96.          }finally {   
  97.              postMethod.releaseConnection();   
  98.          }   
  99.         return sb.toString();   
  100.      }   
  101.     public static void main(String[] str) {   
  102.         //get   
  103.          WebHttpClient util=new WebHttpClient();   
  104.          String content=util.getWebContentByGet("http://www.baidu.com", "gb2312");   
  105.          System.out.println(content);   
  106.         //post   
  107. //       Map<String,String> map=new HashMap<String,String>();   
  108. //       map.put("toPath","toPath");   
  109. //       map.put("action","login");   
  110. //       map.put("loginname","13761083826");   
  111. //       map.put("password","111111");   
  112. //       String content=util.getWebContentByPost("http://localhost:8080/Lottery/login.portal",map, "UTF-8");   
  113. //       System.out.println(content);   
  114.      }   
  115. }  
  116. http://apache.freelamp.com/httpcomponents/commons-httpclient/binary/commons-httpclient-3.1.zip
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章