分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 代码编程

HttpClient跨域请求post

发布时间:2023-09-06 01:29责任编辑:赖小花关键词:跨域

service层

@Override
public Map<String, Object> selectCurrentProgress(String branchesId,String businessId) throws Exception {
//用于结束返回映射结果
Map<String ,Object> map=new HashMap<String,Object>();
//用于判断是否返回成功
int result=0;
???????// 创建默认的httpClient实例. ???
???????CloseableHttpClient httpclient = HttpClients.createDefault(); ?
???????// 创建httppost ???
???????HttpPost httppost = new HttpPost("请求地址"); ?
???????// 创建参数队列 ???
???????List<NameValuePair> formparams = new ArrayList<NameValuePair>(); ?
???????formparams.add(new BasicNameValuePair("branchesId", branchesId));
???????formparams.add(new BasicNameValuePair("businessId", businessId));
???????UrlEncodedFormEntity uefEntity; ?
???????try { ?
???????????uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8"); ?
???????????httppost.setEntity(uefEntity); ?
???????????System.out.println("executing request " + httppost.getURI()); ?
???????????CloseableHttpResponse response = httpclient.execute(httppost); ?
???????????try { ?
???????????????HttpEntity entity = response.getEntity(); ?
???????????????if (entity != null) {
???????????????String jsonStr=EntityUtils.toString(entity, "UTF-8");
???????????????JSONObject jsonObj=JSONObject.parseObject(jsonStr);
???????????????if(jsonObj.getInteger("Code")==0){
???????????????result=1;
???????????????JSONObject dataJSONObj=jsonObj.getJSONObject("Data");
???????????????Integer waitingNumber=dataJSONObj.getInteger("要在页面显示的参数1");
???????????????Integer remainderNumber=dataJSONObj.getInteger("要在页面显示的参数2");
???????????????String estimateWaitingTime=dataJSONObj.getString("要在页面显示的参数3");
???????????????map.put("waitingNumber", waitingNumber);
???????????????map.put("remainderNumber", remainderNumber);
???????????????map.put("EstimateWaitingTime", estimateWaitingTime);
???????????????}
???????????????????System.out.println("--------------------------------------"); ?
??????????????????// System.out.println("Response content: " + EntityUtils.toString(entity, "UTF-8")); ?
???????????????????System.out.println("--------------------------------------"); ?
???????????????}
???????????} finally {
???????????????response.close(); ?
???????????} ?
???????} catch (ClientProtocolException e) { ?
???????????e.printStackTrace(); ?
???????} catch (UnsupportedEncodingException e1) { ?
???????????e1.printStackTrace(); ?
???????} catch (IOException e) { ?
???????????e.printStackTrace(); ?
???????} finally { ?
???????????// 关闭连接,释放资源 ???
???????????try { ?
???????????????httpclient.close(); ?
???????????} catch (IOException e) { ?
???????????????e.printStackTrace(); ?
???????????} ?
???????} ?
???map.put("result", result);
return map;
}

Controller层

@RequestMapping("")
@ResponseBody
public Map<String, Object> selectCurrentProgress(String branchesId,String businessId){
Map<String, Object> map = new HashMap<String,Object>();
try {
map = keywordsService.selectCurrentProgress( branchesId, businessId);
} catch (Exception e) {
map.put("result", 1);
map.put("message", "异常");
e.printStackTrace();
}
return map;
}

第一次写博客,写的不是很好,请见谅

HttpClient跨域请求post

原文地址:http://www.cnblogs.com/Mingzz/p/8017734.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved