發送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);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM