J2EE工具类:WebHttpClient.java


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);
}
}


HTTPClient PostMethod 中文乱码问题解决方案(2种)
[url]http://blog.csdn.net/apei830/archive/2010/04/25/5526236.aspx[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值