httpPost的两种方式
1,post-Body流和post参数,以下客户端代码和服务端代码可共用
客户端代码
/**
* post 方法
* 抛送给EDI
* @param url http://127.0.0.1:9003/api/edi/csm/csmReturnSubConBody?customerId=Fotile_CSM&api=csmreturnsub_confirm&id=6006
* @param jsonParam xml报文结构
* @return
*/
String httpPost45(String url, String jsonParam) {
//url?后面的即为post parmas 参数,bodu 放在数据流中进行传输
CloseableHttpClient httpclient = HttpClients.createDefault()
// HttpGet httpGet = new HttpGet(url)
HttpPost post=new HttpPost(url)
//httpClient 4.5版本的超时参数配置
RequestConfig requestConfig = RequestConfig.custom()
.setConnectTimeout(50000).setConnectionRequestTimeout(50000)
.setSocketTimeout(50000).build()
post.setConfig(requestConfig)
//往BODY里填充数据主体
StringEntity entitys=new StringEntity(jsonParam.toString(), "utf-8")
entitys.setContentEncoding("UTF-8")
entitys.setContentType("application/xml")
post.setEntity(entitys)
HttpResponse response = httpclient.execute(post)
// System.out.println("得到的结果:" + response.getStatusLine())//得到请求结果
String str = EntityUtils.toString(response.getEntity())//得到请求回来的数据
return str
}
客户端代码二=========================================
如果只是简单拼接进url是行不通的,因为我们都知道URLEncoder,对url字符集编码设置,所以需要对所有的值进行字符集编码设置,最终我们封装成了如下post方法支持url拼接入相应的请求参数:
POST_URL:请求url
urlParam:如上需要封装进url的参数
body:普通需要传递的参数
public static String httpURLConnectionPOST (String POST_URL,Map<String, String> urlParam,String body) { CloseableHttpResponse response = null;
try {
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(6000)
.setConnectTimeout(6000)
.setConnectionRequestTimeout(6000)
.build();
//httpclient
CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();
// HttpPost httpPost = new HttpPost(POST_URL);
StringBuilder param=new StringBuilder("");
//将要拼接的参数urlencode
for (String key:urlParam.keySet()){
param.append(key + "=" + URLEncoder.encode(urlParam.get(key), "UTF-8") + "&");
}
//pingjie
HttpPost httpPost = new HttpPost(POST_URL+param.toString());
//请求参数设置
if(com.sf.ccsp.common.util.StringUtils.isNotEmpty(body)){
StringEntity entity=new StringEntity(body, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
}
response = httpclient.execute(httpPost);
HttpEntity entity = response.getEntity();
return EntityUtils.toString(entity, "UTF-8");
} catch (UnsupportedEncodingException e) {
logger.error(e.getMessage(), e);
} catch (ClientProtocolException e) {
logger.error(e.getMessage(), e);
} catch (IOException e) {
logger.error(e.getMessage(), e);
} catch (Exception e){
System.out.println(e);
}finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
} } return null; }
服务端代码
@ResponseBody
@RequestMapping(value = "csmReturnSubConBody", method = RequestMethod.POST, produces = "application/xml")
ResponseMessage csmReturnSubConBody(HttpServletRequest request, HttpServletResponse response,
@RequestParam Map<String, String> params) {
//params为客户端URL?后面的参数集,同理,也可以将bodu放到参数集里,进行传输
CustomerInfo customerInfo = erpSetting.getCustomerInfo(params.customerId as String)
if (!customerInfo) return
ApiInfo apiInfo = erpSetting.getApiInfo(customerInfo, params.api as String)
if (!apiInfo) return
String body = readBody(request)//这里去解析post流里的body数据
ResponseMessage rsp = csmSvc.convertBodyAndSendErpRetu(apiInfo, customerInfo, body, "xml", params.id as Object, null)
return rsp
}
对于post参数流,服务端,可以这样取值
String body = params.keySet()[0] + "=" + params[params.keySet()[0]].toString()
params.keySet()[0]得到key
params[params.keySet()[0]].toString()得到第一个key的value
OLY电子标签项目
httpPost的两种方式的更多相关文章
- 在基于MVC的Web项目中使用Web API和直接连接两种方式混合式接入
在我之前介绍的混合式开发框架中,其界面是基于Winform的实现方式,后台使用Web API.WCF服务以及直接连接数据库的几种方式混合式接入,在Web项目中我们也可以采用这种方式实现混合式的接入方式 ...
- Android请求服务器的两种方式--post, get的区别
android中用get和post方式向服务器提交请求_疯狂之桥_新浪博客http://blog.sina.com.cn/s/blog_a46817ff01017yxt.html Android提交数 ...
- HTTP/HTTPS GET&POST两种方式的实现方法
关于GET及POST方式的区别请参照前面文章:http://www.cnblogs.com/hunterCecil/p/5698604.html http://www.cnblogs.com/hunt ...
- 比较两种方式的form请求提交
[一]浏览器form表单提交 表单提交, 适用于浏览器提交.像常见的pc端的网银支付,用户在商户商城购买商品,支付时商家系统把交易数据通过form表单提交到三方支付网关,然后用户在三方网关页面完成支付 ...
- Android提交数据到服务器的两种方式四种方法
本帖最后由 yanghe123 于 2012-6-7 09:58 编辑 Android应用开发中,会经常要提交数据到服务器和从服务器得到数据,本文主要是给出了利用http协议采用HttpClient方 ...
- js实现页面跳转的两种方式
CreateTime--2017年8月24日08:13:52Author:Marydon js实现页面跳转的两种方式 方式一: window.location.href = url 说明:我们常用 ...
- Struts2实现ajax的两种方式
基于Struts2框架下实现Ajax有两种方式,第一种是原声的方式,另外一种是struts2自带的一个插件. js部分调用方式是一样的: JS代码: function testAjax() { var ...
- CSharpGL(18)分别处理glDrawArrays()和glDrawElements()两种方式下的拾取(ColorCodedPicking)
CSharpGL(18)分别处理glDrawArrays()和glDrawElements()两种方式下的拾取(ColorCodedPicking) 我在(Modern OpenGL用Shader拾取 ...
- 两种方式实现java生成Excel
Web应用中难免会遇到需要将数据导出并生成excel文件的需求.同样,对于本博客中的总结,也是建立在为了完成这样的一个需求,才开始去了解其实现形式,并且顺利完成需求的开发,先将实现过程总结于此.本博文 ...
随机推荐
- Codeforces Round #649 (Div. 2) B. Most socially-distanced subsequence (数学,差分)
题意:有一长度为\(n\)的数组,求一子序列,要求子序列中两两差的绝对值最大,并且子序列尽可能短. 题解:将数组看成坐标轴上的点,其实就是求每个单调区间的端点,用差分数组来判断单调性. 代码: #in ...
- Educational Codeforces Round 89 (Rated for Div. 2) B. Shuffle (数学,区间)
题意:有长为\(n\)的排列,其中\(x\)位置上的数为\(1\),其余位置全为\(0\),询问\(m\)次,每次询问一个区间,在这个区间内可以交换任意两个位置上的数,问\(1\)最后出现在不同位置的 ...
- 13. 从0学ARM-Cortex-A9 RTC裸机程序编写
一.RTC RTC(Real-Time Clock) 实时时钟. RTC是集成电路,通常称为时钟芯片.在一个嵌入式系统中,通常采用RTC来提供可靠的系统时间,包括时分秒和年月日等,而且要求在系统处于关 ...
- Cobbler服务引导第三方PE系统
通过Cobbler服务引导第三方PE系统 1.上传第三方ios到/root/Ushendu_win10.iso并增加菜单项 cobbler distro add --name=Ushendu_win1 ...
- 获取txt编码方式
在操作txt的时候,有时会出现乱码,这是因为没有使用正确的编码方式来操作txt,我们需要先获取txt的编码方式,再进行读写操作.下面是获取txt编码的方法: /// <summary> / ...
- PAT L2-020 功夫传人【BFS】
一门武功能否传承久远并被发扬光大,是要看缘分的.一般来说,师傅传授给徒弟的武功总要打个折扣,于是越往后传,弟子们的功夫就越弱-- 直到某一支的某一代突然出现一个天分特别高的弟子(或者是吃到了灵丹.挖到 ...
- python文件持久化存储
文件持久化存储 目录 文件持久化存储 脑图 文件的操作 with 语句 OS模块 json模块 存储为Excel文件 脑图 文件的操作 import os import platform # 1. 获 ...
- TensorFlow & Machine Learning
TensorFlow & Machine Learning TensorFlow 实战 传统方式 规则 + 数据集 => 答案 无监督学习 机器学习 神经元网络 答案 + 数据集 =&g ...
- lua windows上使用vs编译Lua
video 下载lua源文件 还有种方法: 创建空项目,取名lua,导入lua源文件 将luac.c移除,然后编译lua.exe 将lua.c移除,添加luac.c然后编译lua.exe后重命名位lu ...
- perl 在windows上获取当前桌面壁纸
更多 #!/usr/bin/perl # 在windows获取当前的桌面壁纸 # See also: https://www.winhelponline.com/blog/find-current-w ...