HttpClient
HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,
并且它支持 HTTP 协议最新的版本和建议。
使用httpClient执行get请求<url不带参的>
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class HttpClient {
@Test
public void doGet()throws Exception{
//创建一个HttpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个GET对象
HttpGet get = new HttpGet("http://www.sogou.com");
//执行请求
CloseableHttpResponse response = httpClient.execute(get);
//获取相应结果
int statusCode = response.getStatusLine().getStatusCode();//响应的状态码
System.out.println(statusCode);//如果结果为200 即为正常
HttpEntity entity = response.getEntity();//响应内容
String string = EntityUtils.toString(entity);
System.out.println(string);
//关闭HttpClient
response.close();
httpClient.close();
}
}
使用httpClient执行get请求<url带参的>
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class HttpClient {
@Test
public void doGetWithParam()throws Exception{
//创建一个httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个uri对象
URIBuilder uriBuilder = new URIBuilder("http://www.sogou.com/web");
uriBuilder.addParameter("query", "杀破狼"); //www.sogou.com/web?query=杀破狼
HttpGet get = new HttpGet(uriBuilder.build());
//执行结果
CloseableHttpResponse response = httpClient.execute(get);
//获取响应结果
int statusCode = response.getStatusLine().getStatusCode();
System.out.println(statusCode);
HttpEntity entity = response.getEntity();
String string = EntityUtils.toString(entity,"UTF-8");
System.out.println(string);
//关闭HttpClient
response.close();
httpClient.close();
}
}
HttpClient的post请求方式<不带参数的post请求>
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class HttpClient {
@Test
public void doPost()throws Exception{
//创建一个httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/httpclient/post.html");
//执行结果
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
//关闭HttpClient
response.close();
httpClient.close();
}
}
HttpClient使用post请求<带参数请求>
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
public class HttpClient {
@Test
public void doPostWithParam()throws Exception{
//创建一个httpClient对象
CloseableHttpClient httpClient = HttpClients.createDefault();
//创建一个post对象
HttpPost post = new HttpPost("http://localhost:8080/httpclient/post.html");
//创建一个Entity.模拟一个表单
List<NameValuePair> kvList = new ArrayList<>();
kvList.add(new BasicNameValuePair("username","abc"));
kvList.add(new BasicNameValuePair("password","123"));
//包装成一个Entity对象
StringEntity entity = new UrlEncodedFormEntity(kvList);
//设置请求内容
post.setEntity(entity);
//执行结果
CloseableHttpResponse response = httpClient.execute(post);
String string = EntityUtils.toString(response.getEntity());
System.out.println(string);
//关闭HttpClient
response.close();
httpClient.close();
}
}