httpclient作用
在java代码中,发送Http请求。通常用来实现远程接口调用。
HttpClient测试
在工程中添加httpclient的pom依赖。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
1.执行GET请求
public static void doGet(){
// 1.创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 2.创建http GET请求
HttpGet httpGet = new HttpGet("http://www.oschina.net/");
CloseableHttpResponse response = null;
try {
// 3.执行请求
response = httpclient.execute(httpGet);
System.out.println(response.getStatusLine());
// 4.判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println("内容长度:" + content.length());
}
}catch(Exception e){
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
2.执行GET带参数
public static void doGetParam(){
// 1.创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
// 2.定义请求的参数
URI uri = newURIBuilder("http://www.baidu.com/s").setParameter("wd", "数据库").build();
// 3.创建http GET请求
HttpGet httpGet = new HttpGet(uri);
// 4.执行请求
response = httpclient.execute(httpGet);
// 5.判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
}catch(Exception e){
e.printStackTrace();
}finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
3.执行post请求
public static void doPost(){
// 1.创建Httpclient对象
CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
// 2.创建http POST请求
HttpPost httpPost = new HttpPost("http://www.oschina.net/");
CloseableHttpResponse response = null;
try {
// 3.执行请求
response = httpclient.execute(httpPost);
System.out.println(response.getStatusLine());
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
}catch(Exception e){
e.printStackTrace();
}finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.执行post带参数
public static void doPostParam() throws Exception{
// 1.创建Httpclient对象
CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
// 2.创建http POST请求
HttpPost httpPost = new HttpPost("http://www.oschina.net/search");
//3.给post 设置请求头(重中之重,否则被调用服务接收不到参数,且需要用jsonObject接收)
httpPost.setHeader("Content-Type","application/json");
//4. 给post设置参数
JSONObject param = new JSONObject();
StringEntity entity = new String(param.toString());
//entity 设置字符编码,防止乱码
Entity.setContentEncoding("UTF-8");
// 将请求实体参数设置到httpPost对象中
httpPost.setEntity(entity);
CloseableHttpResponse response = null;
try {
// 执行请求
response = httpclient.execute(httpPost);
System.out.println(response.getStatusLine());
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(content);
}
} finally {
if (response != null) {
response.close();
}
httpclient.close();
}
}