谈到httpclient的话,只要会想到apache的httpclient和jetty的httpclient,但是apache的httpclient3和4之间又有区别,通过学些,最终总结了三种方式使用HttpClient,
分别为使用httpclient3,httpclient4,jetty的httpclient,下面分别来贴代码:
第1种:使用的jar包为commons-httpclient-3.1,只需要一个jar包即可
这里使用的是GetMethod,与httpcleint4有区别
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public
static
void
main(String[] args) {
HttpClient httpClient =
new
HttpClient();
//打开窗口
GetMethod getMethod =
new
GetMethod(
"http://www.baidu.com/"
); //输入网址
try
{
int
statusCode = httpClient.executeMethod(getMethod);
//按下回车运行,得到返回码
System.out.println(statusCode);
if
(statusCode != HttpStatus.SC_OK) {
System.err.println(
"Method failed: "
+ getMethod.getStatusLine());
}
//读取内容
byte
[] responseBody = getMethod.getResponseBody();
//得到返回的内容
//处理内容
System.out.println(
new
String(responseBody));
}
catch
(Exception e) {
e.printStackTrace();
}
finally
{
getMethod.releaseConnection();
}
}
|
第二种,使用的jar包为httpclient4,需要的jar包由httpclient3分成了多个,自己去下载
注意这里使用的是HttpGet,而httpclient3使用的是GetMethod
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
public
static
void
main(String[] args) {
HttpClient httpClient =
new
DefaultHttpClient();
HttpGet httpGet =
new
HttpGet(
"http://www.baidu.com"
);
// 打印请求信息
System.out.println(httpGet.getRequestLine());
try
{
// 发送请求,返回响应
HttpResponse response =httpClient.execute(httpGet);
// 打印响应信息
System.out.println(response.getStatusLine());
HttpEntity httpEntity = response.getEntity();
System.out.println( httpEntity.getContentType());
System.out.println(httpEntity.getContentLength());
System.out.println(EntityUtils.getContentCharSet(httpEntity));
InputStream in = httpEntity.getContent();
//可以得到请求的内容了
.....
//这里可以自由发挥了
}
catch
(Exception e) {
}
}
|
第三种:使用jetty的httpclient,jar在jetty包里面的lib包里面
可以分为同步和异步两种
异步:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
public
static
void
main(String[] args) {
HttpClient client =
new
HttpClient();
client.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
//还可以进行其他的配置
try
{
client.start();
}
catch
(Exception e) {
e.printStackTrace();
}
ContentExchange exchange =
new
ContentExchange()
//exchange,里面放置回调方法
{
protected
void
onResponseComplete()
throws
IOException
{
super
.onResponseComplete();
String responseContent =
this
.getResponseContent();
System.out.println(responseContent);
........
//可以对返回结果自由发挥了
}
};
exchange.setMethod(
"GET"
);
exchange.setURL(
"http://www.baidu.com"
);
try
{
client.send(exchange);
//client发送消息
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
|
同步:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
HttpClient httpClient =
new
HttpClient();
...........................
//同样对client进行配置
try
{
httpClient.start();
ContentExchange contentExchange =
new
ContentExchange();
httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
contentExchange.setURL(
"http://www.baidu.com"
);
contentExchange.setTimeout(
30000
);
httpClient.send(contentExchange);
//client发送
contentExchange.waitForDone();
//同步等待结果返回
System.err.println(
"Response status: "
+contentExchange.getResponseStatus());
System.out.println(
"Response content:"
+contentExchange.getResponseContent());
}
catch
(Exception e) {
e.printStackTrace();
}
|
希望对初学者有所帮助!!!!!
本文转自布拉君君 51CTO博客,原文链接:http://blog.51cto.com/5148737/1413767,如需转载请自行联系原作者