当前位置: 首页 > news >正文

Java Post请求参数格式为XML

方式一:
public static void PostXml1(String url, String xml) throws IOException {OkHttpClient client = new OkHttpClient().newBuilder().build();//okhttp3.MediaType mediaType = okhttp3.MediaType.parse("application/xml");okhttp3.MediaType mediaType = okhttp3.MediaType.parse("text/xml");//RequestBody body = RequestBody.create(mediaType, "<messages xmlns=\"http://www.neusoft.com/hit/rhin\">\r\n    <heartbeat>\r\n        0\r\n    </heartbeat>\r\n    <switchset>\r\n        <authority>\r\n            <authoritytype>\r\n                0\r\n            </authoritytype>\r\n            <username/>\r\n            <userpwd/>\r\n            <license/>\r\n        </authority>\r\n        <visitor>\r\n            <!-- 调用方22位机构编码 -->\r\n            <sourceorgan>\r\n                3301060000000000000000\r\n            </sourceorgan>\r\n            <!-- 调用方10位接入系统编码 -->\r\n            <sourcedomain>\r\n                3301000013\r\n            </sourcedomain>\r\n        </visitor>\r\n        d\r\n        <serviceinf>\r\n            <servicecode>\r\n                XBSJCJCJJ:PCRWHHQJ\r\n            </servicecode>\r\n        </serviceinf>\r\n        <provider>\r\n            <targetorgan/>\r\n            <targetdomain/>\r\n        </provider>\r\n        <route/>\r\n        <process/>\r\n    </switchset>\r\n    <business>\r\n        <standardcode/>\r\n        <requestset>\r\n            <reqcondition/>\r\n            <reqpaging>\r\n                0\r\n            </reqpaging>\r\n            <reqpageindex>\r\n                -1\r\n            </reqpageindex>\r\n            <reqpageset>\r\n                0\r\n            </reqpageset>\r\n        </requestset>\r\n        <datacompress>\r\n            0\r\n        </datacompress>\r\n        <daqtaskid>\r\n            20231109000000001\r\n        </daqtaskid>\r\n        <businessdata>\r\n            <!--声明类型 0:总声明/1:单次声明-->\r\n            <declaretype>\r\n                0\r\n            </declaretype>\r\n            <!--采集类型 0:增量采集-->\r\n            <collecttype>\r\n                0\r\n            </collecttype>\r\n            <!--声明门(急)诊挂号登记业务上传6月6号一天增量数据的情况 -->\r\n            <totaldeclare>\r\n                <!--交换标准编码 示例:门(急)诊挂号登记-->\r\n                <colrescode>\r\n                    REQ.C0101.0302.02\r\n                </colrescode>\r\n                <!--任务数 -->\r\n                <tasknum>\r\n                    5\r\n                </tasknum>\r\n                <!--数据开始时间 -->\r\n                <begindatetime>\r\n                    20221124000000\r\n                </begindatetime>\r\n                <!--数据结束时间 -->\r\n                <enddatetime>\r\n                    20221124235959\r\n                </enddatetime>\r\n                <!--一个数据集的整体描述 -->\r\n                <tdeclare>\r\n                    <!--门(急)诊挂号登记-->\r\n                    <setcode>\r\n                        C0101.0302.02\r\n                    </setcode>\r\n                    <!--记录数 -->\r\n                    <datanum>\r\n                        500\r\n                    </datanum>\r\n                </tdeclare>\r\n            </totaldeclare>\r\n        </businessdata>\r\n        <returnmessage>\r\n            <retcode/>\r\n            <rettext/>\r\n        </returnmessage>\r\n    </business>\r\n    <extendset/>\r\n</messages>");RequestBody body = RequestBody.create(mediaType, "<messages xmlns=\"http://www.neusoft.com/hit/rhin\"><heartbeat>0</heartbeat><switchset><authority><authoritytype>0</authoritytype><username/><userpwd/><license/></authority><visitor><!-- 调用方22位机构编码 --><sourceorgan>3301060000000000000000</sourceorgan><!-- 调用方10位接入系统编码 --><sourcedomain>3301000013</sourcedomain></visitor>d<serviceinf><servicecode>XBSJCJCJJ:PCRWHHQJ</servicecode></serviceinf><provider><targetorgan/><targetdomain/></provider><route/><process/></switchset><business><standardcode/><requestset><reqcondition/><reqpaging>0</reqpaging><reqpageindex>-1</reqpageindex><reqpageset>0</reqpageset></requestset><datacompress>0</datacompress><daqtaskid>20231109000000001</daqtaskid><businessdata><!--声明类型 0:总声明/1:单次声明-->\r\n            <declaretype>\r\n                0\r\n            </declaretype>\r\n            <!--采集类型 0:增量采集-->\r\n            <collecttype>\r\n                0\r\n            </collecttype>\r\n            <!--声明门(急)诊挂号登记业务上传6月6号一天增量数据的情况 -->\r\n            <totaldeclare>\r\n                <!--交换标准编码 示例:门(急)诊挂号登记-->\r\n                <colrescode>\r\n                    REQ.C0101.0302.02\r\n                </colrescode>\r\n                <!--任务数 -->\r\n                <tasknum>\r\n                    5\r\n                </tasknum>\r\n                <!--数据开始时间 -->\r\n                <begindatetime>20221124000000</begindatetime><!--数据结束时间 --><enddatetime>20221124235959</enddatetime><!--一个数据集的整体描述 --><tdeclare><!--门(急)诊挂号登记--><setcode>C0101.0302.02</setcode><!--记录数 --><datanum>500</datanum></tdeclare></totaldeclare></businessdata><returnmessage><retcode/><rettext/></returnmessage></business><extendset/></messages>");//RequestBody body = RequestBody.create(mediaType, "");Request request = new Request.Builder().url("https://www.baidu.com/sc/totalDeclare?short-access=aaa68ed6397a4595b4d3e1c37533b6ac").method("POST", body).addHeader("Content-Type", "text/xml")//.addHeader("short-access-token", "aaa68ed6397a4595b4d3e1c37533b6ac").build();
Response response = client.newCall(request).execute();String responseBody = response.body().toString();System.out.println(responseBody);}
方式二:
private String invoke(String requestUrl, String requestXml) throws Exception {StringBuilder builder = new StringBuilder();HttpURLConnection connection = getHttpURLConnection(requestUrl);// 输出流OutputStream outputStream = connection.getOutputStream();outputStream.write(requestXml.getBytes(StandardCharsets.UTF_8));outputStream.close();// 输入流InputStream inputStream = connection.getInputStream();InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);BufferedReader bufferedReader = new BufferedReader(inputStreamReader);String line = null;while ((line = bufferedReader.readLine()) != null) {builder.append(line);}bufferedReader.close();inputStreamReader.close();inputStream.close();connection.disconnect();return builder.toString();
}/*** 获取HttpURLConnection*/
private HttpURLConnection getHttpURLConnection(String requestUrl) throws Exception {URL url = new URL(requestUrl);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setConnectTimeout(3000);connection.setReadTimeout(3000);connection.setDoOutput(true);connection.setDoInput(true);connection.setUseCaches(false);connection.setRequestMethod("POST");connection.setRequestProperty("accept", "*/*");connection.setRequestProperty("connection", "Keep-Alive");connection.setRequestProperty("Content-type", "application/xml");	return connection;
}
方式三:
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.13</version>
</dependency>
public static String postXmlRequest(String url, String xml) throws Exception {HttpPost post = new HttpPost(url);post.setHeader("Content-type", "text/xml");//post.setEntity(new StringEntity(xml));post.setEntity(new StringEntity(xml, StandardCharsets.UTF_8));CloseableHttpClient client = HttpClients.createDefault();CloseableHttpResponse response = client.execute(post);return response.getStatusLine().getStatusCode() == 200 ? EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8) : null;
}
http://www.lryc.cn/news/225739.html

相关文章:

  • Windows 安装 JDK 8 和 JDK 17 和多版本JDK切换
  • SpringData、SparkStreaming和Flink集成Elasticsearch
  • 中国电子学会2023年09月份青少年软件编程Python等级考试试卷六级真题(含答案)
  • 基于STM32设计的智能水母投喂器(华为云IOT)
  • 合成数据加速机器视觉学习
  • 物业管理服务预约小程序的效果如何
  • ORA-00257: Archiver error. Connect AS SYSDBA only until resolved错误解决
  • backbone:从AlexNet到...(持续补充ing)
  • FiRa标准——MAC实现(二)
  • oracle中分组函数LISTAGG
  • 深度学习pytorch之hub模块
  • LeetCode 2258. 逃离火灾:BFS
  • C# PaddleInference.PP-HumanSeg 人像分割 替换背景色
  • Java 变量初始化的两种方式和优缺点比较
  • 15.三数之和
  • 竞赛选题 深度学习疲劳驾驶检测 opencv python
  • PROFINET和UDP、MODBUS-RTU通信速度对比实验
  • CSS3 多媒体查询、网格布局
  • SpringBoot基础(九)-- 配置文件优先级
  • C++ static关键字
  • Anaconda Powershell Prompt和Anaconda Prompt的区别
  • 关于tcp发送成功但对端无法接收情况的思考
  • 01-解码-H264转YUV
  • keepalived+Nginx+邮件
  • CMakeCache.txt有什么用
  • ZYNQ_project:key_breath
  • 设计模式 (原则)
  • LeetCode 每日一题 2023/11/6-2023/11/12
  • Linux 基于 LVM 逻辑卷的磁盘管理【简明教程】
  • CTFHUB-WEB-SQL注入