我要用java后台模拟一个post请求,请求一个页面,但测试了N久都没有成功,整了三天了,找不着问题所在,很是恼火,我CaoTM HttpClient 和 HttpUrlConnection。
我的离奇遭遇是这样的:在eclipse里用tomcat跑一个应用,然后写个测试类用HttpClient访问这个应用中的一个页面,然而WEB应用请求是收到了,但WEB应用那个jsp 在request.getParameter("XXX");取模拟过来的请求的参数时报超时错误。取不到!
我的测试类代码如下:import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.params.HttpParams;
import org.apache.commons.httpclient.util.URIUtil; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map; public class HttpHelper {
private static Log log = LogFactory.getLog(HttpHelper.class);
/**
* 执行一个HTTP POST请求,返回请求响应的HTML
*
* @param url 请求的URL地址
* @param params 请求的查询参数,可以为null
* @param charset 字符集
* @param requestHeader 请求头
* @return 返回请求响应的HttpMethod
*/
public static HttpMethod doPost(String url, Map <String, String> params, String charset,java.util.List requestHeader) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
client.setConnectionTimeout(10000*3);
client.setTimeout(10000*3);
HttpParams d;
HttpMethod method = new PostMethod(url);
//设置Http Post数据
if (params != null) {
HttpMethodParams p = new HttpMethodParams();
for (Map.Entry<String, String> entry : params.entrySet()) {
p.setParameter(entry.getKey(), entry.getValue());
}
p.setContentCharset("UTF-8");
method.setParams(p);
}
if(requestHeader!=null)
{
for(int i=0;i<requestHeader.size();i++){
Header h3 = (Header)requestHeader.get(i);
method.setRequestHeader(h3 );
}
}
try {
client.executeMethod(method);
return method;
} catch (IOException e) {
log.error("执行HTTP Post请求" + url + "时,发生异常!", e);
} finally {
}
return null;
} public static void main(String[] args) {
java.util.Map map = new java.util.HashMap();
map.put("UserName", "admin"); //添加POST模拟参数
System.out.println();
java.util.List list = new java.util.ArrayList();
Header header = null;
header = new Header("accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
list.add(header);
header = new Header("referer","http://localhost/contract/login.jsp");
list.add(header);
header = new Header("accept-language","zh-cn");
list.add(header);
header = new Header("content-type","application/x-www-form-urlencoded");
list.add(header);
header = new Header("ua-cpu","x86");
list.add(header);
header = new Header("accept-encoding","gzip, deflate");
list.add(header);
header = new Header("user-agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)");
list.add(header);
header = new Header("host","localhost");
list.add(header);
header = new Header("connection","Keep-Alive");
list.add(header);
header = new Header("cache-control","no-cache");
list.add(header);
header = new Header("content-length","14");
list.add(header);
HttpMethod y = doPost("http://localhost/contract/logincookies.jsp", map, "UTF-8", list);
try {
System.out.println(y.getStatusCode()+" "+ y.getResponseBodyAsString());
y.releaseConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
网上把baidu google 翻遍也没人碰到我这样的问题 CaoTM HttpClient 和 HttpUrlConnection。 估计也没人能帮助我,CaoTM 怎么这么背
我的离奇遭遇是这样的:在eclipse里用tomcat跑一个应用,然后写个测试类用HttpClient访问这个应用中的一个页面,然而WEB应用请求是收到了,但WEB应用那个jsp 在request.getParameter("XXX");取模拟过来的请求的参数时报超时错误。取不到!
我的测试类代码如下:import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.URIException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.commons.httpclient.params.HttpParams;
import org.apache.commons.httpclient.util.URIUtil; import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Map; public class HttpHelper {
private static Log log = LogFactory.getLog(HttpHelper.class);
/**
* 执行一个HTTP POST请求,返回请求响应的HTML
*
* @param url 请求的URL地址
* @param params 请求的查询参数,可以为null
* @param charset 字符集
* @param requestHeader 请求头
* @return 返回请求响应的HttpMethod
*/
public static HttpMethod doPost(String url, Map <String, String> params, String charset,java.util.List requestHeader) {
StringBuffer response = new StringBuffer();
HttpClient client = new HttpClient();
client.setConnectionTimeout(10000*3);
client.setTimeout(10000*3);
HttpParams d;
HttpMethod method = new PostMethod(url);
//设置Http Post数据
if (params != null) {
HttpMethodParams p = new HttpMethodParams();
for (Map.Entry<String, String> entry : params.entrySet()) {
p.setParameter(entry.getKey(), entry.getValue());
}
p.setContentCharset("UTF-8");
method.setParams(p);
}
if(requestHeader!=null)
{
for(int i=0;i<requestHeader.size();i++){
Header h3 = (Header)requestHeader.get(i);
method.setRequestHeader(h3 );
}
}
try {
client.executeMethod(method);
return method;
} catch (IOException e) {
log.error("执行HTTP Post请求" + url + "时,发生异常!", e);
} finally {
}
return null;
} public static void main(String[] args) {
java.util.Map map = new java.util.HashMap();
map.put("UserName", "admin"); //添加POST模拟参数
System.out.println();
java.util.List list = new java.util.ArrayList();
Header header = null;
header = new Header("accept","image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*");
list.add(header);
header = new Header("referer","http://localhost/contract/login.jsp");
list.add(header);
header = new Header("accept-language","zh-cn");
list.add(header);
header = new Header("content-type","application/x-www-form-urlencoded");
list.add(header);
header = new Header("ua-cpu","x86");
list.add(header);
header = new Header("accept-encoding","gzip, deflate");
list.add(header);
header = new Header("user-agent","Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 1.1.4322)");
list.add(header);
header = new Header("host","localhost");
list.add(header);
header = new Header("connection","Keep-Alive");
list.add(header);
header = new Header("cache-control","no-cache");
list.add(header);
header = new Header("content-length","14");
list.add(header);
HttpMethod y = doPost("http://localhost/contract/logincookies.jsp", map, "UTF-8", list);
try {
System.out.println(y.getStatusCode()+" "+ y.getResponseBodyAsString());
y.releaseConnection();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
网上把baidu google 翻遍也没人碰到我这样的问题 CaoTM HttpClient 和 HttpUrlConnection。 估计也没人能帮助我,CaoTM 怎么这么背
解决方案 »
- 超难问题,关于html中输入的问题(能解决的绝对是高手~)
- [100分求助]jsp数组传递和保存到数据库的问题。
- socket通讯,已读到流末尾了,为什么没返回-1,一直阻塞
- 通过javabean查询数据库出错
- 各位大哥,小弟,在做毕业设计不知道哪们大哥有(互联网通知汇报系统)的相关文档或者说信息发布平台的相关文档
- 如何用JAVA实现对搜索网站查询结果的收集
- 一个有关与邮件系统的问题
- 【请教】这样的设计会不会存在什么问题呢?
- Lucene添加索引问题,如何判断要加的索引已经存在???给分啦
- 请问各位朋友,因以后工作需要需学习JAVA,应怎样入门学习较好,先看哪些书????
- struts2 文件下载
- JSF+A4J 省市二级联动
String result = null;
HttpClient httpClient = new HttpClient();
GetMethod getMethod = new GetMethod(url);
//使用系统提供的默认的恢复策略
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
try {
int statusCode = httpClient.executeMethod(getMethod);
if (statusCode != HttpStatus.SC_OK) {
System.out.println("httpget err:"+getMethod.getStatusLine());
}
InputStream in = getMethod.getResponseBodyAsStream();
result = streamToString(in);
} catch (HttpException e) {
System.out.println("httpget err:"+e.getMessage());
} catch (IOException e) {
System.out.println("httpget err:"+e.getMessage());
} finally {
getMethod.releaseConnection(); //释放连接
}
return result;
}
private static String streamToString(InputStream in) {
StringBuffer sb = new StringBuffer();
BufferedReader br =
new BufferedReader(new InputStreamReader(in));
String strLine = "";
try {
while ((strLine = br.readLine()) != null) {
sb.append(strLine.trim());
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
------------------------
调用httpget即可,你要保证传入的url有正确的能得到的返回值。比如是response.getWriter().print写的东西