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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值