java httpclient 跨域_跨域请求httpclient

packagecom.jt.common.service;importjava.util.ArrayList;importjava.util.List;importjava.util.Map;importorg.apache.http.NameValuePair;importorg.apache.http.client.config.RequestConfig;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.entity.StringEntity;importorg.apache.http.impl.client.CloseableHttpClient;importorg.apache.http.message.BasicNameValuePair;importorg.apache.http.util.EntityUtils;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.stereotype.Service;

@Servicepublic classHttpClientService {private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientService.class);

@Autowired(required=false)privateCloseableHttpClient httpClient;

@Autowired(required=false)privateRequestConfig requestConfig;/*** 执行get请求

*

*@paramurl

*@return*@throwsException*/

public String doGet(String url,Map params,String encode) throwsException {

LOGGER.info("执行GET请求,URL = {}", url);if(null !=params){

URIBuilder builder= newURIBuilder(url);for (Map.Entryentry : params.entrySet()) {

builder.setParameter(entry.getKey(), entry.getValue());

}

url=builder.build().toString();

}//创建http GET请求

HttpGet httpGet = newHttpGet(url);

httpGet.setConfig(requestConfig);

CloseableHttpResponse response= null;try{//执行请求

response =httpClient.execute(httpGet);//判断返回状态是否为200

if (response.getStatusLine().getStatusCode() == 200) {if(encode == null){

encode= "UTF-8";

}returnEntityUtils.toString(response.getEntity(), encode);

}

}finally{if (response != null) {

response.close();

}//此处不能关闭httpClient,如果关闭httpClient,连接池也会销毁

}return null;

}public String doGet(String url, String encode) throwsException{return this.doGet(url, null, encode);

}public String doGet(String url) throwsException{return this.doGet(url, null, null);

}/*** 带参数的get请求

*

*@paramurl

*@paramparams

*@return*@throwsException*/

public String doGet(String url, Map params) throwsException {return this.doGet(url, params, null);

}/*** 执行POST请求

*

*@paramurl

*@paramparams

*@return*@throwsException*/

public String doPost(String url, Map params,String encode) throwsException {//创建http POST请求

HttpPost httpPost = newHttpPost(url);

httpPost.setConfig(requestConfig);if (null !=params) {//设置2个post参数,一个是scope、一个是q

List parameters = new ArrayList(0);for (Map.Entryentry : params.entrySet()) {

parameters.add(newBasicNameValuePair(entry.getKey(), entry.getValue()));

}//构造一个form表单式的实体

UrlEncodedFormEntity formEntity = null;if(encode!=null){

formEntity= newUrlEncodedFormEntity(parameters,encode);

}else{

formEntity= newUrlEncodedFormEntity(parameters);

}//将请求实体设置到httpPost对象中

httpPost.setEntity(formEntity);

}

CloseableHttpResponse response= null;try{//执行请求

response =httpClient.execute(httpPost);//判断返回状态是否为200

if (response.getStatusLine().getStatusCode() == 200) {return EntityUtils.toString(response.getEntity(), "UTF-8");

}

}finally{if (response != null) {

response.close();

}

}return null;

}/*** 执行POST请求

*

*@paramurl

*@paramparams

*@return*@throwsException*/

public String doPost(String url, Map params) throwsException {//创建http POST请求

HttpPost httpPost = newHttpPost(url);

httpPost.setConfig(requestConfig);if (null !=params) {//设置2个post参数,一个是scope、一个是q

List parameters = new ArrayList(0);for (Map.Entryentry : params.entrySet()) {

parameters.add(newBasicNameValuePair(entry.getKey(), entry.getValue()));

}//构造一个form表单式的实体

UrlEncodedFormEntity formEntity = newUrlEncodedFormEntity(parameters);//将请求实体设置到httpPost对象中

httpPost.setEntity(formEntity);

}

CloseableHttpResponse response= null;try{//执行请求

response =httpClient.execute(httpPost);//判断返回状态是否为200

if (response.getStatusLine().getStatusCode() == 200) {return EntityUtils.toString(response.getEntity(), "UTF-8");

}

}finally{if (response != null) {

response.close();

}

}return null;

}public String doPostJson(String url, String json) throwsException {//创建http POST请求

HttpPost httpPost = newHttpPost(url);

httpPost.setConfig(requestConfig);if(null !=json){//设置请求体为 字符串

StringEntity stringEntity = new StringEntity(json,"UTF-8");

httpPost.setEntity(stringEntity);

}

CloseableHttpResponse response= null;try{//执行请求

response =httpClient.execute(httpPost);//判断返回状态是否为200

if (response.getStatusLine().getStatusCode() == 200) {return EntityUtils.toString(response.getEntity(), "UTF-8");

}

}finally{if (response != null) {

response.close();

}

}return null;

}

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值