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 #479 (Div. 3) E. Cyclic Components (思维,DFS)
题意:给你\(n\)个顶点和\(m\)条边,问它们有多少个单环(无杂环),例如图中第二个就是一个杂环. 题解:不难发现,如果某几个点能够构成单环,那么每个点一定只能连两条边.所以我们先构建邻接表,然后 ...
- Codeforces Round #653 (Div. 3) E1. Reading Books (easy version) (贪心,模拟)
题意:有\(n\)本书,A和B都至少要从喜欢的书里面读\(k\)本书,如果一本书两人都喜欢的话,那么他们就可以一起读来节省时间,问最少多长时间两人都能够读完\(k\)本书. 题解:我们可以分\(3\) ...
- 使用 Tye 辅助开发 k8s 应用竟如此简单(二)
续上篇,这篇我们来进一步探索 Tye 更多的使用方法.本篇我们来了解一下如何在 Tye 中使用服务发现. Newbe.Claptrap 是一个用于轻松应对并发问题的分布式开发框架.如果您是首次阅读本系 ...
- cin 与 getline
cin空格截断 getline(cin,s) 换行结束 ....太愚蠢了
- codeforces 7B
B. Memory Manager time limit per test 1 second memory limit per test 64 megabytes input standard inp ...
- 云原生系列1 pod基础
POD解决了什么问题? 成组资源调度问题的解决. mesos采用的资源囤积策略容易出现死锁和调度效率低下问题:google采用的乐观调度技术难度非常大: 而k8s使用pod优雅的解决了这个问题. po ...
- Web 前端如何一键开启上帝模式
Web 前端如何一键开启上帝模式 God Mode document.designMode = `on`; refs https://www.cnblogs.com/xgqfrms/tag/desig ...
- Creative Commons : CC (知识共享署名 授权许可)
1 https://creativecommons.org/ Keep the internet creative, free and open. Creative Commons help ...
- LeetCode 算法面试题汇总
LeetCode 算法面试题汇总 算法面试题 https://leetcode-cn.com/problemset/algorithms/ https://leetcode-cn.com/proble ...
- vue & table with operation slot
vue & table with operation slot seed demo <!-- @format --> <template> <seed ref=& ...