发送xml报文去第三方请求获取xml报文数据

时间:2023-03-09 20:49:04
发送xml报文去第三方请求获取xml报文数据
import java.io.*;

import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection; public class UrlTest { void testPost(String urlStr) {
try {
URL url = new URL(urlStr);
URLConnection con = url.openConnection();
con.setDoOutput(true);
con.setRequestProperty("Pragma:", "no-cache");
con.setRequestProperty("Cache-Control", "no-cache");
con.setRequestProperty("Content-Type", "text/xml;charset=gbk");
OutputStreamWriter out = new OutputStreamWriter(con.getOutputStream());
out.write(new String(getXmlInfo().getBytes("gbk")));
out.flush();
out.close();
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
String msg = "";
while ((msg = br.readLine()) != null) {
System.out.println(msg);
} } catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} private String getXmlInfo() {
String body = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<Packet>" +
" <Head>" +
" <Password>666666</Password>" +
" <RequestType>V010</RequestType>" +
" <User>shandong_gd</User>" +
" </Head>" +
" <Body>" +
" <vehicle>" +
" <autoModelCode></autoModelCode>" +
" <usageAttributeCode>1</usageAttributeCode>" +
" <vehicleFrameNo></vehicleFrameNo>" +
" <vehicleName>BMW</vehicleName>" +
" </vehicle>" +
" </Body>" +
"</Packet>";
return body;
} public static void main(String[] args) {
String url = "需要输入对应的接口地址";
new UrlTest().testPost(url);
}
}