J2EE工具类:WebHttpClient.java
来源:互联网 发布:网络管理岗位说明书 编辑:程序博客网 时间:2024/06/03 13:33
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.Map;
- import java.util.Set;
- import org.apache.commons.httpclient.HttpClient;
- import org.apache.commons.httpclient.HttpException;
- import org.apache.commons.httpclient.HttpStatus;
- import org.apache.commons.httpclient.NameValuePair;
- import org.apache.commons.httpclient.methods.GetMethod;
- import org.apache.commons.httpclient.methods.PostMethod;
- public class WebHttpClient {
- /**
- * 获得网页中的所有HTML内容
- * @param url
- * @param charset
- * @return
- */
- public String getWebContentByGet(String url,String charset){
- HttpClient client = new HttpClient();
- GetMethod getMethod = new GetMethod(url);
- StringBuilder sb = new StringBuilder();
- try {
- // 状态码
- int statusCode=client.executeMethod(getMethod);
- if (statusCode == HttpStatus.SC_OK) {
- //获得HTML文本
- BufferedReader bf = new BufferedReader(new InputStreamReader(
- getMethod.getResponseBodyAsStream(), charset));
- String line = null;
- while ((line = bf.readLine()) != null) {
- sb.append(line).append("/r/n");
- }
- bf.close();
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- getMethod.releaseConnection();
- }
- return sb.toString();
- }
- /**
- * 获得网页中的所有HTML内容
- * @param url
- * @param mapData:传递的参数
- * @param charset
- * @return
- */
- public String getWebContentByPost(String url,Map<String,String> mapData,String charset){
- HttpClient client = new HttpClient();
- PostMethod postMethod = new PostMethod(url);
- StringBuilder sb = new StringBuilder();
- // 填入各个表单域的值
- NameValuePair[] data = new NameValuePair[mapData.size()];
- Set set = mapData.entrySet();
- Iterator iterator = set.iterator();
- int i=0;
- while (iterator.hasNext()) {
- Map.Entry entry = (Map.Entry) iterator.next();
- data[i]=new NameValuePair((String)entry.getKey(),(String)entry.getValue());
- i++;
- }
- /*
- NameValuePair[] data = {new NameValuePair("toPath","toPath"),
- new NameValuePair("action","login"),
- new NameValuePair("loginname","13761083826"),
- new NameValuePair("password","111111")
- };
- */
- // 将表单的值放入postMethod中
- postMethod.setRequestBody(data);
- try {
- int statusCode = client.executeMethod(postMethod);
- if (statusCode == HttpStatus.SC_OK) {
- //获得HTML文本
- BufferedReader bf = new BufferedReader(new InputStreamReader(
- postMethod.getResponseBodyAsStream(), charset));
- String line = null;
- while ((line = bf.readLine()) != null) {
- sb.append(line).append("/r/n");
- }
- bf.close();
- }
- } catch (HttpException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }finally {
- postMethod.releaseConnection();
- }
- return sb.toString();
- }
- public static void main(String[] str) {
- //get
- WebHttpClient util=new WebHttpClient();
- String content=util.getWebContentByGet("http://www.baidu.com", "gb2312");
- System.out.println(content);
- //post
- // Map<String,String> map=new HashMap<String,String>();
- // map.put("toPath","toPath");
- // map.put("action","login");
- // map.put("loginname","13761083826");
- // map.put("password","111111");
- // String content=util.getWebContentByPost("http://localhost:8080/Lottery/login.portal",map, "UTF-8");
- // System.out.println(content);
- }
- }
- http://apache.freelamp.com/httpcomponents/commons-httpclient/binary/commons-httpclient-3.1.zip
- J2EE工具类:WebHttpClient.java
- J2EE工具类:WebHttpClient.java
- J2EE工具类:DateUtil .java
- J2EE工具类:CookieUtil.java
- J2EE工具类:NumberUtil.java
- J2EE工具类:DateUtil .java
- J2EE工具类:StringUtil.java
- J2EE工具类:PropertiesUtil.java
- J2EE工具类:DataBaseUtil.java
- J2EE工具类:ArrayUtil.java
- J2EE工具类:WeatherUtil.java
- J2EE工具类:DES.java
- J2EE工具类:WebClient.java
- J2EE工具类:CalendarUtil .java
- J2EE工具类:ClientSocketUtil.java&ServerSocketConnection.java
- J2EE工具类:MoneyUtil.java小写钱转成大写钱
- J2EE上传文件工具类
- java编程工具 JDK(或J2EE)的安装
- ubuntu10.4 ibus-pinyin安装
- Heritrix URL乱码
- Heritrix 动态URL
- RTP Payload Format for H.264 Video
- 北京网商服务平台评出首批重点企业 扶持力度大增
- J2EE工具类:WebHttpClient.java
- 打印html中指定内容
- 构建互联网信任体系 提升用户应用体验
- H.263 H.263+ Payload Type
- 国土资源部关于发布《市(地)级土地利用总体规划数据库标准》等3项推荐性行业标准的公告
- 破解网络经济下的信任困局 可信网站演变之路
- 破解网络经济下的信任困局 可信网站演变之路
- struts2整合JFreeChart
- 用于MPEG-4视听流的RTP负载格式