分享web开发知识

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

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

httpPost的两种方式

发布时间:2023-09-06 02:31责任编辑:傅花花关键词:http

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
???}

服务端代码

@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
???}

httpPost的两种方式

原文地址:https://www.cnblogs.com/joker331/p/10303204.html

知识推荐

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