方式一:
public static void PostXml1(String url, String xml) throws IOException {OkHttpClient client = new OkHttpClient().newBuilder().build();okhttp3.MediaType mediaType = okhttp3.MediaType.parse("text/xml");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>");Request request = new Request.Builder().url("https://www.baidu.com/sc/totalDeclare?short-access=aaa68ed6397a4595b4d3e1c37533b6ac").method("POST", body).addHeader("Content-Type", "text/xml").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();
}
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, 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;
}