HTTP协议是现在Internet上使用的最多的、最重要的协议了,越来越多的Java应用程序需要直接通过HTTP协议来访问网络资源。虽然在JDK的java net包中已经提供了访问HTTP协议的基本功能,但是对于大部分的应用程序来说,JDK库本身提供的功能还不够丰富和灵活。
HttpClient是Apache Jakarta Common下的子项目,可以用来提供高效的、最新的、功能丰富的支持Http协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议。
HttpClient主要提供的功能:
1.实现了所有HTTP的方法,GET,POST,PUT,HEAD等;
2.支持自动转向;
3.支持HTTPS协议;
4.支持代理服务器等。
对于早期的HttpClient(大概在3.0左右),使用HttpClient需要以下6个步骤:
1.创建HttpClient的实例,new一个对象出来,大部分的时候HttpClient默认的构造函数已经足够使用;
2.创建某种连接方法的实例,也就是对于HTTP的方法,创建他们的实例,如new一个GetMethod、PostMethod。然后可以在他们的构造函数中传入待连接的地址,不传也行;
3.调用第一步中创建好的实例的execute方法来执行第二步中创建好的method实例;
4.读response;
5.释放连接。无论执行方法释放成功,都必须释放连接;
6.处理得到后的内容。
根据以上步骤,来编写GET方法获取某网页内容(http://www.baidu.com/,实际上百度的地址是https的)的代码。
需要引入几个jar包.commons-httpclient-3.0.jar和logging和codec.
代码具体内容如下:
package com.tgb.httpclient;
import java.io.IOException;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
/**
* HttpClient中get方法
* @author liuyanling
*
*/
public class GetMethodTest {
public static void main(String[] args) {
getMethod();
}
private static void getMethod() {
//1.创建HttpClient
HttpClient httpClient = new HttpClient();
//2.创建连接的某种方法
GetMethod getMethod = new GetMethod("http://www.baidu.com/");
getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,new DefaultHttpMethodRetryHandler());
try {
//3.执行
int statusCode = httpClient.executeMethod(getMethod);
if(statusCode != HttpStatus.SC_OK) {
System.out.println("get method failed: " + getMethod.getStatusLine());
}
//4.读response
byte[] responseBody = getMethod.getResponseBody();
//6.处理得到的内容,此处为打印
System.out.println(new String(responseBody,"utf-8"));
} catch (HttpException e) {
System.out.println("Please check your provided http address!");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.无论如何,释放连接
getMethod.releaseConnection();
}
}
}
最后处理到的效果,(只是其中部分,并通过工具格式化)
<html>
<head></head>
<body link="#0000cc">
十一月 08, 2015 6:30:56 下午 org.apache.commons.httpclient.HttpMethodBase getResponseBody 警告: Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.
<!--STATUS OK-->
<meta http-equiv="content-type" content="text/html;charset=utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=Edge" />
<meta content="always" name="referrer" />
<meta name="theme-color" content="#2932e1" />
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon" />
<link rel="search" type="application/opensearchdescription+xml" href="/content-search.xml" title="百度搜索" />
<link rel="icon" sizes="any" mask="" href="//www.baidu.com/img/baidu.svg" />
<link rel="dns-prefetch" href="//s1.bdstatic.com" />
<link rel="dns-prefetch" href="//t1.baidu.com" />
<link rel="dns-prefetch" href="//t2.baidu.com" />
<link rel="dns-prefetch" href="//t3.baidu.com" />
<link rel="dns-prefetch" href="//t10.baidu.com" />
<link rel="dns-prefetch" href="//t11.baidu.com" />
<link rel="dns-prefetch" href="//t12.baidu.com" />
<link rel="dns-prefetch" href="//b1.bdstatic.com" />
<title>百度一下,你就知道</title>
...
而变成html文件打开之后的效果是这样的,可以看出确实是拿到了百度的页面.
然后是POST方法.
根据RFC2616,对POST的解释如下:POST方法用来向目的服务器发出请求,要求目的服务器接受被附在请求后的实体,并把实体当做请求队列(Request-Line)中请求URI所指定资源的附加新子项.POST被设计成用统一的方法实现下列功能:
1.对现有资源的注释(Annotation of existing resources)
2.向电子公告栏、新闻组、邮件列表或类似讨论组发送消息
3.提交数据块,如将表单的结果提交给数据处理过程
4.通过附加操作来扩展数据库
这里构造PostMethod和之前构造GetMethod的方式一样,也需要URI参数。这次,我们用PostMethod来做提交表单中的数据块。假设是一个登录功能,需要提交用户名和密码,这是两个域。在表单中用NameValuePair类来表示域,该类的构造函数第一个参数是域名,第二个参数是域值。然后将域设置到PostMethod中,使用setRequestBody.最后,由于登陆后会自动跳到的另一个页面,我们可以控制不自动跳转,等我们处理完,再做转向处理。
package com.tgb.httpclient;
import java.io.IOException;
import org.apache.commons.httpclient.Header;
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.PostMethod;
public class PostMethodTest {
public static void main(String[] args) {
postMethod();
}
private static void postMethod() {
//1.创建HttpClient
HttpClient httpClient = new HttpClient();
//2.创建连接的某种方法
PostMethod postMethod = new PostMethod("http://www.newsmth.net/bbslogin2.php");
NameValuePair[] data = { new NameValuePair("id", "youUserName"),new NameValuePair("passwd", "yourPwd") };
postMethod.setRequestBody(data);
try {
//3.执行
int statusCode = httpClient.executeMethod(postMethod);
if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode ==HttpStatus.SC_MOVED_TEMPORARILY) {
//4.读response
Header locationHeader = postMethod.getResponseHeader("location");
//6.处理得到的内容
String location = "";
if(locationHeader != null) {
location = locationHeader.getValue();
System.out.println("The page was redirected to: "+location);
} else {
System.out.println("Location field value was null");
}
}
return;
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//5.无论如何,释放连接
postMethod.releaseConnection();
}
}
}
得到的结果为:php页面登陆完毕之后,是要跳转到http://www.newsmth.net/errors/abuse.html 这个页面。
The page was redirected to: http://www.newsmth.net/errors/abuse.html
十一月 08, 2015 7:44:06 下午 org.apache.commons.httpclient.HttpMethodDirector isRedirectNeeded
信息: Redirect requested but followRedirects is disabled
简单的GetMethod和PostMethod方法就介绍完了,使用起来不复杂,记住6步即可,接下来还有一些常见问题需要介绍.