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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-14
猜你喜欢
  • 2022-12-23
  • 2021-10-12
  • 2022-12-23
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
相关资源
相似解决方案