当前位置:首页 > 编程笔记 > 正文
已解决

Java Post请求参数格式为XML

来自网友在路上 184884提问 提问时间:2023-11-11 08:01:13阅读次数: 84

最佳答案 问答题库848位专家为你答疑解惑

方式一:
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;
}
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"Java Post请求参数格式为XML":http://eshow365.cn/6-37438-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!