J2EE工具类:WebHttpClient.java
来源:互联网 发布:js清空页面div中的内容 编辑:程序博客网 时间:2024/06/05 16:12
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 blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}finally {postMethod.releaseConnection();}return sb.toString();}public static void main(String[] str) {//getWebHttpClient 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);}}
HTTPClient PostMethod 中文乱码问题解决方案(2种)
http://blog.csdn.net/apei830/archive/2010/04/25/5526236.aspx
0 0
- 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)的安装
- 使用免费快递接口查询物流信息
- 浏览器报:org.springframework.beans.factory.NoSuchBeanDefinitionException
- hdu2481 Toy
- htmlparser API
- 两种方式创建多线程
- J2EE工具类:WebHttpClient.java
- android面试题
- J2EE工具类:WebClient.java
- 十个数的数组(作业)
- linux中ldconfig的使用介绍
- J2EE工具类:ClientSocketUtil.java&ServerSocketConnection.java
- 归并排序
- HtmlparseUtil.java
- DESPlus+Base64+MD5Util