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, "\r\n    \r\n        0\r\n    \r\n    \r\n        \r\n            \r\n                0\r\n            \r\n            \r\n            \r\n            \r\n        \r\n        \r\n            \r\n            \r\n                3301060000000000000000\r\n            \r\n            \r\n            \r\n                3301000013\r\n            \r\n        \r\n        d\r\n        \r\n            \r\n                XBSJCJCJJ:PCRWHHQJ\r\n            \r\n        \r\n        \r\n            \r\n            \r\n        \r\n        \r\n        \r\n    \r\n    \r\n        \r\n        \r\n            \r\n            \r\n                0\r\n            \r\n            \r\n                -1\r\n            \r\n            \r\n                0\r\n            \r\n        \r\n        \r\n            0\r\n        \r\n        \r\n            20231109000000001\r\n        \r\n        \r\n            \r\n            \r\n                0\r\n            \r\n            \r\n            \r\n                0\r\n            \r\n            \r\n            \r\n                \r\n                \r\n                    REQ.C0101.0302.02\r\n                \r\n                \r\n                \r\n                    5\r\n                \r\n                \r\n                \r\n                    20221124000000\r\n                \r\n                \r\n                \r\n                    20221124235959\r\n                \r\n                \r\n                \r\n                    \r\n                    \r\n                        C0101.0302.02\r\n                    \r\n                    \r\n                    \r\n                        500\r\n                    \r\n                \r\n            \r\n        \r\n        \r\n            \r\n            \r\n        \r\n    \r\n    \r\n");
        RequestBody body = RequestBody.create(mediaType, "0033010600000000000000003301000013dXBSJCJCJJ:PCRWHHQJ0-10020231109000000001\r\n            \r\n                0\r\n            \r\n            \r\n            \r\n                0\r\n            \r\n            \r\n            \r\n                \r\n                \r\n                    REQ.C0101.0302.02\r\n                \r\n                \r\n                \r\n                    5\r\n                \r\n                \r\n                2022112400000020221124235959C0101.0302.02500");
        //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;
}

你可能感兴趣的:(java,xml,开发语言)