一、介绍
httpClient是Apache公司的一个子项目, 用来提高高效的、最新的、功能丰富的支持http协议的客户端编程工具包。完成可以模拟浏览器发起请求行为。
二、简单使用例子 : 模拟浏览器发起访问谷歌首页请求
1、pom.xml 配置
<dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient</artifactId> <version>3.1</version> </dependency>
2、示例
import java.io.IOException; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpMethod; import org.apache.commons.httpclient.methods.GetMethod; public class HttpClientDemo { public static void main(String[] args) throws HttpException, IOException { HttpClient httpClient = new HttpClient(); String uri = "https://www.google.com"; HttpMethod method = new GetMethod(uri); httpClient.executeMethod(method ); //服务器返回状态 print(method.getStatusLine()); //返回的内容 print(method.getResponseBodyAsString()); //释放连接 method.releaseConnection(); } static void print(Object o) { System.out.println(o); } }
打印出来的结果是这样子的
时间: 2024-10-11 06:10:09