Method 1:
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\\
<heartbeat>\ r\\
0\r\\
</heartbeat>\r\\
<switchset>\r\\
<authority>\r\\
<authoritytype>\r\\
0\ \r\\
</authoritytype>\r\\
<username/>\r\\
<userpwd/>\r\\
<license/>\r\\
</authority>\ \r\\
<visitor>\r\\
<!-- Caller 22-bit organization code -->\r\\
<sourceorgan>\r\\
3301060000000000000000\r\\
< /sourceorgan>\r\\
<!-- Caller's 10-digit access system code-->\r\\
<sourcedomain>\r\\
3301000013\r\\
</sourcedomain> \r\\
</visitor>\r\\
d\r\\
<serviceinf>\r\\
<servicecode>\r\\
XBSJCJCJJ:PCRWHHQJ\r\\
< /servicecode>\r\\
</serviceinf>\r\\
<provider>\r\\
<targetorgan/>\r\\
<targetdomain/>\r\\
</ provider>\r\\
<route/>\r\\
<process/>\r\\
</switchset>\r\\
<business>\r\\
<standardcode/ >\r\\
<requestset>\r\\
<reqcondition/>\r\\
<reqpaging>\r\\
0\r\\
</reqpaging>\r\ \
<reqpageindex>\r\\
-1\r\\
</reqpageindex>\r\\
<reqpageset>\r\\
0\r\\
</reqpageset>\ \r\\
</requestset>\r\\
<datacompress>\r\\
0\r\\
</datacompress>\r\\
<daqtaskid>\r\\
20231109000000001\r\\
</daqtaskid>\r\\
<businessdata>\r\\
<!--Declaration type 0: Total statement/1: Single statement-->\r\ n <declaretype>\r\\
0\r\\
</declaretype>\r\\
<!--Collection type 0: incremental collection-->\r\\
<collecttype> \r\\
0\r\\
</collecttype>\r\\
<!--Declare the situation of uploading one-day incremental data on June 6 for outpatient (emergency) clinic registration business--> \r\\
<totaldeclare>\r\\
<!--Exchange standard coding example: outpatient (emergency) clinic registration-->\r\\
<colrescode>\r\\
REQ .C0101.0302.02\r\\
</colrescode>\r\\
<!--Number of tasks-->\r\\
<tasknum>\r\\
5\r\ n </tasknum>\r\\
<!--Data start time-->\r\\
<begindatetime>\r\\
20221124000000\r\\
</begindatetime>\r \\
<!--Data end time-->\r\\
<enddatetime>\r\\
20221124235959\r\\
</enddatetime>\r\\
<!--One Overall description of the data set-->\r\\
<tdeclare>\r\\
<!--Outpatient (emergency) clinic registration-->\r\\
<setcode>\r\ \
C0101.0302.02\r\\
</setcode>\r\\
<!--Number of records-->\r\\
<datanum>\r\\
500\r\ \
</datanum>\r\\
</tdeclare>\r\\
</totaldeclare>\r\\
</businessdata>\r\\
<returnmessage>\r\ n <retcode/>\r\\
<retext/>\r\\
</returnmessage>\r\\
</business>\r\\
<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><!-- The 22-digit institution code of the caller --><sourceorgan>3301060000000000000000</sourceorgan>< !-- Caller's 10-digit access system code--><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><!--Declaration type 0: Total statement/1: Single statement- ->\r\\
<declaretype>\r\\
0\r\\
</declaretype>\r\\
<!--Collection type 0: incremental collection-->\ r\\
<collecttype>\r\\
0\r\\
</collecttype>\r\\
<!--Statement that outpatient (emergency) clinic registration business will be uploaded on June 6th. The situation of quantitative data-->\r\\
<totaldeclare>\r\\
<!--Example of exchange standard coding: outpatient (emergency) clinic registration-->\r\\
<colrescode> \r\\
REQ.C0101.0302.02\r\\
</colrescode>\r\\
<!--Number of tasks-->\r\\
<tasknum>\r\ n 5\r\\
</tasknum>\r\\
<!--Data start time-->\r\\
<begindatetime>20221124000000</begindatetime><!--Data end time- -><enddatetime>20221124235959</enddatetime><!--Overall description of a data set--><tdeclare><!--Outpatient (emergency) clinic registration--><setcode>C0101.0302.02</setcode> <!--Number of records--><datanum>500</datanum></tdeclare></totaldeclare></businessdata><returnmessage><retcode/><retext/></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);
}
Method 2:
private String invoke(String requestUrl, String requestXml) throws Exception {<!-- -->
StringBuilder builder = new StringBuilder();
HttpURLConnection connection = getHttpURLConnection(requestUrl);
//output stream
OutputStream outputStream = connection.getOutputStream();
outputStream.write(requestXml.getBytes(StandardCharsets.UTF_8));
outputStream.close();
// input stream
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();
}
/**
* Get HttpURLConnection
*/
private HttpURLConnection getHttpURLConnection(String requestUrl) throws Exception {<!-- -->
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
\t
connection.setConnectTimeout(3000);
connection.setReadTimeout(3000);
\t
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setUseCaches(false);
\t
connection.setRequestMethod("POST");
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("Content-type", "application/xml");
return connection;
}
Method 3:
<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;
}