http请求pom 客户端_HttpClient 远程接口调用方式

这是一个Java类,使用Apache HttpClient库实现GET和POST方法的HTTP请求。HttpClientUtils提供了doGet和doPost静态方法,用于向指定URL发送请求,并处理响应。在POST请求中,它还支持设置请求参数和处理重定向策略。
摘要由CSDN通过智能技术生成

packageorg.chu.ego.base.utils;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importorg.apache.http.HttpEntity;importorg.apache.http.NameValuePair;importorg.apache.http.client.entity.UrlEncodedFormEntity;importorg.apache.http.client.methods.CloseableHttpResponse;importorg.apache.http.client.methods.HttpGet;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.client.utils.URIBuilder;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.impl.client.HttpClientBuilder;importorg.apache.http.impl.client.HttpClients;importorg.apache.http.impl.client.LaxRedirectStrategy;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;/*** --发送get请求的api

* CloseableHttpClient类 ,client实现类

* HttpClients类 ,client工具类,用于创建客户端对象。

* CloseableHttpResponse接口,请求的响应对象

* URIBuilder类 :url构建类,用于设置get请求的路径变量

* HttpGet类 :get请求的发送对象

* EntityUtils类 实体处理类

*

* --发送post 请求使用的api

* CloseableHttpClient类

* HttpClientBuilder client构建对象,用于创建客户端对象。

* LaxRedirectStrategy类,post请求重定向的策略

* CloseableHttpResponse 请求的响应对象

* HttpPost post请求的发送对象

* NameValuePair 类,用于设置参数值

* UrlEncodedFormEntity:用于设置表单参数给发送对象HttpPost

*

*@authorranger

**/

public classHttpClientUtils {public static String doGet(String url,Mapparams){//获取httpclient客户端

CloseableHttpClient httpclient =HttpClients.createDefault();

String resultString= "";

CloseableHttpResponse response= null;try{

URIBuilder builder= newURIBuilder(url);if(null!=params){for(String key:params.keySet()) {

builder.setParameter(key, params.get(key));

}

}

HttpGet get= newHttpGet(builder.build());

response=httpclient.execute(get);

System.out.println(response.getStatusLine());if(200==response.getStatusLine().getStatusCode()){

HttpEntity entity=response.getEntity();

resultString= EntityUtils.toString(entity, "utf-8");

}

}catch(Exception e) {

e.printStackTrace();

}finally{if(null!=response){try{

response.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}if(null!=httpclient){try{

httpclient.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}returnresultString;

}public staticString doGet(String url){return doGet(url, null);

}public static String doPost(String url,Mapparams){/*** 在4.0及以上httpclient版本中,post需要指定重定向的策略,如果不指定则按默认的重定向策略。

*

* 获取httpclient客户端*/CloseableHttpClient httpclient= HttpClientBuilder.create().setRedirectStrategy( newLaxRedirectStrategy()).build();

String resultString= "";

CloseableHttpResponse response= null;try{

HttpPost post= newHttpPost(url);

List paramaters = new ArrayList<>();if(null!=params){for(String key : params.keySet()) {

paramaters.add(newBasicNameValuePair(key,params.get(key)));

}

UrlEncodedFormEntity formEntity= newUrlEncodedFormEntity (paramaters);

post.setEntity(formEntity);

}/*** HTTP/1.1 403 Forbidden

* 原因:

* 有些网站,设置了反爬虫机制

* 解决的办法:

* 设置请求头,伪装浏览器*/post.addHeader("user-agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");

response=httpclient.execute(post);

System.out.println(response.getStatusLine());if(200==response.getStatusLine().getStatusCode()){

HttpEntity entity=response.getEntity();

resultString= EntityUtils.toString(entity, "utf-8");

}

}catch(Exception e) {

e.printStackTrace();

}finally{if(null!=response){try{

response.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}if(null!=httpclient){try{

httpclient.close();

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

}

}returnresultString;

}public staticString doPost(String url){return doPost(url, null);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值