HttpClient的使用方法案例
2,950 阅读
HttpClient 是 Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
以下是我写的一个httpclient的测试程序,供大家参考:
package crawlerTest;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.util.EntityUtils;
public class httpTest {
public static void main(String[] args) throws IOException {
HttpClient client = new DefaultHttpClient(); //初始化httpclient
String personalUrl="http://www.autohome.com.cn/beijing/"; //指定一个种子url
HttpGet postMethod = new HttpGet(personalUrl); //post方法请求
System.out.println("postMethod====="+ postMethod+"=====");
(personalUrl);
System.out.println(+getMethod+);
(HttpVersion.HTTP_1_1,
HttpStatus.SC_OK, );
response = client.execute(getMethod);
System.out.println(+response+);
response.getStatusLine().getStatusCode();
System.out.println(+StatusCode+);
System.out.println(response);
(StatusCode == ){
EntityUtils.toString (response.getEntity(),);
System.out.println(entity);
EntityUtils.consume(response.getEntity());
} {
EntityUtils.consume(response.getEntity());
}
}
}
