//get请求
HttpClient client = new HttpClient();
GetMethod method = new GetMethod(Url);//Url请求地址
// 设置 Http 连接超时为5秒
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
// 设置 Http 读取超时为10秒
client.getHttpConnectionManager().getParams().setSoTimeout(10000);
try {
method.addRequestHeader("Content-Type",
"application/x-www-form-urlencoded;charset=UTF-8");
// 设置请求重试处理,用的是默认的重试处理:请求三次
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler());
//new DefaultHttpMethodRetryHandler() 里面参数设置请求次数和是否创建新请求 因英文不行,借助翻译工具大体猜测是这个意思
// 执行请求
client.executeMethod(method);
if (method.getStatusLine().getStatusCode() == 200) { //这是测试简单的网站
buf.append(method.getResponseBodyAsString());
}
System.out.println(buf.toString());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 释放连接
method.releaseConnection();
}
//部分网站大体都有转向或者重定向
状态码 对应HttpServletResponse的常量 详细描述
301 SC_MOVED_PERMANENTLY 页面已经永久移到另外一个新地址
302 SC_MOVED_TEMPORARILY 页面暂时移动到另外一个新的地址
303 SC_SEE_OTHER 客户端请求的地址必须通过另外的URL来访问
307 SC_TEMPORARY_REDIRECT 同SC_MOVED_TEMPORARILY
原文:
大体思路是这样根据实际情况处理
//检查是否重定向
int statuscode = post.getStatusCode();
if ((statuscode == HttpStatus.SC_MOVED_TEMPORARILY) ||
(statuscode == HttpStatus.SC_MOVED_PERMANENTLY) ||
(statuscode == HttpStatus.SC_SEE_OTHER) ||
(statuscode == HttpStatus.SC_TEMPORARY_REDIRECT)) {
//读取新的URL地址
Header header = post.getResponseHeader("location");
if (header != null) {
String newuri = header.getValue();
if ((newuri == null) || (newuri.equals("")))
newuri = "/";
GetMethod redirect = new GetMethod(newuri);
client.executeMethod(redirect);
System.out.println("Redirect:"+ redirect.getStatusLine().toString());
redirect.releaseConnection();
} else
System.out.println("Invalid redirect");
}
//post 请求
StringBuffer buf = new StringBuffer();
HttpClient client = new HttpClient();
PostMethod method = new PostMethod(Url);//请求地址
// 设置 Http 连接超时为5秒
client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
// 设置Http 读取超时为10秒
method.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 10000);
/*// 设置 Http 连接超时为10秒
client.getHttpConnectionManager().getParams().setConnectionTimeout(10000);
// 设置 Http 读取超时为10秒
client.getHttpConnectionManager().getParams().setSoTimeout(10000);*/
try {
//method.addRequestHeader("Content-Type","application/x-www-form-urlencoded;charset=UTF-8");
// 设置POST参数
NameValuePair[] nameValuePair = {new NameValuePair("id","123"), new NameValuePair("userId","123") };
method.setRequestBody(nameValuePair);
HttpMethodParams param = method.getParams();
// 设置页面编码
// param.setHttpElementCharset("utf-8");
param.setContentCharset("utf-8");
// 设置请求重试处理,用的是默认的重试处理:请求三次
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
// 执行请求
client.executeMethod(method);
System.out.println(method.getStatusLine().getStatusCode());
if (method.getStatusLine().getStatusCode() == 200) {
// 打印返回的信息
// String result = method.getResponseBodyAsString();
InputStream stream = method.getResponseBodyAsStream();
BufferedReader br = new BufferedReader(new InputStreamReader(
stream, "utf-8"));
String line;
while (null != (line = br.readLine())) {
buf.append(line);
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// 释放连接
method.releaseConnection();
}
简单的httpclient到此为止,虽然纠结了很久查看了很多前辈的代码,但是发现都是特例。
1、创建httpclient
2、创建post或者get(必须要设置请求和等待时间,否则程序会陷入卡死状态或者一些莫名其妙的错误)
3、组织请求(主要是post)
4、发送请求(注意字符编码)
5、获取返回解析
返回有几种接收返回数据可以根据不同的内容选择,如getResponseBodyAsString、getResponseBodyAsStream等。
6、关闭httpclient(必须要关闭)
下次等有时间弄cookie的,那个也非常简单。
大道至简~~~