您当前的位置: 首页 >  I.T10001 http

网站跨域解决方案-httpclient(3)

I.T10001 发布时间:2020-01-12 17:25:14 ,浏览量:2

前端用同一个域名和端口,后端转发httpclient请求(或RPC)

本机host配置:

1、后端代码:

2、前端代码:

httpclient工具类

public class HttpClientUtils {

private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class); // 日志记录

 

private static RequestConfig requestConfig = null;

 

static {

// 设置请求和传输超时时间

requestConfig = RequestConfig.custom().setSocketTimeout(2000).setConnectTimeout(2000).build();

}

 

/**

 * post请求传输json参数

 *

 * @param url

 *            url地址

 * @param json

 *            参数

 * @return

 */

public static JSONObject httpPost(String url, JSONObject jsonParam) {

// post请求返回结果

CloseableHttpClient httpClient = HttpClients.createDefault();

JSONObject jsonResult = null;

HttpPost httpPost = new HttpPost(url);

// 设置请求和传输超时时间

httpPost.setConfig(requestConfig);

try {

if (null != jsonParam) {

// 解决中文乱码问题

StringEntity entity = new StringEntity(jsonParam.toString(), "utf-8");

entity.setContentEncoding("UTF-8");

entity.setContentType("application/json");

httpPost.setEntity(entity);

}

CloseableHttpResponse result = httpClient.execute(httpPost);

// 请求发送成功,并得到响应

if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

String str = "";

try {

// 读取服务器返回过来的json字符串数据

str = EntityUtils.toString(result.getEntity(), "utf-8");

// 把json字符串转换成json对象

jsonResult = JSONObject.parseObject(str);

} catch (Exception e) {

logger.error("post请求提交失败:" + url, e);

}

}

} catch (IOException e) {

logger.error("post请求提交失败:" + url, e);

} finally {

httpPost.releaseConnection();

}

return jsonResult;

}

 

/**

 * post请求传输String参数 例如:name=Jack&sex=1&type=2

 * Content-type:application/x-www-form-urlencoded

 *

 * @param url

 *            url地址

 * @param strParam

 *            参数

 * @return

 */

public static JSONObject httpPost(String url, String strParam) {

// post请求返回结果

CloseableHttpClient httpClient = HttpClients.createDefault();

JSONObject jsonResult = null;

HttpPost httpPost = new HttpPost(url);

httpPost.setConfig(requestConfig);

try {

if (null != strParam) {

// 解决中文乱码问题

StringEntity entity = new StringEntity(strParam, "utf-8");

entity.setContentEncoding("UTF-8");

entity.setContentType("application/x-www-form-urlencoded");

httpPost.setEntity(entity);

}

CloseableHttpResponse result = httpClient.execute(httpPost);

// 请求发送成功,并得到响应

if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

String str = "";

try {

// 读取服务器返回过来的json字符串数据

str = EntityUtils.toString(result.getEntity(), "utf-8");

// 把json字符串转换成json对象

jsonResult = JSONObject.parseObject(str);

} catch (Exception e) {

logger.error("post请求提交失败:" + url, e);

}

}

} catch (IOException e) {

logger.error("post请求提交失败:" + url, e);

} finally {

httpPost.releaseConnection();

}

return jsonResult;

}

 

/**

 * 发送get请求

 *

 * @param url

 *            路径

 * @return

 */

public static JSONObject httpGet(String url) {

// get请求返回结果

JSONObject jsonResult = null;

CloseableHttpClient client = HttpClients.createDefault();

// 发送get请求

HttpGet request = new HttpGet(url);

request.setConfig(requestConfig);

try {

CloseableHttpResponse response = client.execute(request);

 

// 请求发送成功,并得到响应

if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {

// 读取服务器返回过来的json字符串数据

HttpEntity entity = response.getEntity();

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

// 把json字符串转换成json对象

jsonResult = JSONObject.parseObject(strResult);

} else {

logger.error("get请求提交失败:" + url);

}

} catch (IOException e) {

logger.error("get请求提交失败:" + url, e);

} finally {

request.releaseConnection();

}

return jsonResult;

}

 

}

关注
打赏
1688896170
查看更多评论

I.T10001

暂无认证

  • 2浏览

    0关注

    154博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0569s