HttpClient simple turorial

this from HttpClient document.
HttpClinet now version is 3.1 ,come from apache.
it dependcy lib: commons loging,commons codec

How to use HttpClinet
1.create an instance of HttpClient
2.create an instance of one of the methods
3.tell the HttpClient to execute the method
4.read the response
5.release the connection
6.deal with the response

Notice that we go through the entire process regardless of whether
the server returned an error or not. This is important because
Http 1.1 allows multiple requests to use the same connection by
simply sending the requests one after another. Obviously, if we
don't read the entire response to the first request, the left over
data will get in the way of the second response. HttpClient try to
handle this but to avoid problem ,it is important to always the
connection.


HttpClient client = new HttpClient();

HttpMethod method = new GetMethod("http://www.google.cn");


executeMethod()
Since the network connection are unreliable. We also deal with any
errors that occur.
executeMethod() throws 2 type exception, HttpException,IOException

HttpException is sub class of IOException, it represents a fatal error
, it usually can't be recoverd.

a plain IOException represents a transport error. so per default HttpClient
will try to recover the request automatilly , default 3 times.

read the response
It's vital that the response body is always read regardless of the status returned by the server.
3 ways to do this:

method.getResponseBody(); return a byte array
method.getResponseBodyAsString(); return a String. conversion from bytes to String using default encoding.
method.getResponseBodyAsStream(); this method is best if it is possible for a lot of data to be received.
as it can be buffered to a file or
processed as it is read.

release the connection
This is a crucial step. We must tell HttpClient that we are done with
the connection .


method.releaseConnection();


all code for this:


import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.*;

public class HttpClientTutorial {

private static String url = "http://www.google.cn/";

public static void main(String[] args) {
// Create an instance of HttpClient.
HttpClient client = new HttpClient();

// Create a method instance.
GetMethod method = new GetMethod(url);

// Provide custom retry handler is necessary
method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
new DefaultHttpMethodRetryHandler(3, false));

try {
// Execute the method.
int statusCode = client.executeMethod(method);

if (statusCode != HttpStatus.SC_OK) {
System.err.println("Method failed: " + method.getStatusLine());
}

// Read the response body.
byte[] responseBody = method.getResponseBody();

// Deal with the response.
// Use caution: ensure correct character encoding and is not binary data
System.out.println(new String(responseBody));

} catch (HttpException e) {
System.err.println("Fatal protocol violation: " + e.getMessage());
e.printStackTrace();
} catch (IOException e) {
System.err.println("Fatal transport error: " + e.getMessage());
e.printStackTrace();
} finally {
// Release the connection.
method.releaseConnection();
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值