爱玺玺

爱玺玺的生活日记本。wx:lb87626

java 模拟 post request payload

post 请求

先贴代码


public String sendCPICPostRequest(String requestUrl, String cookStr, String licenceNo, String referer, String host) {


    String payload = "{\"meta\":{},\"redata\":{\"ecarvo\":{\"plateNo\":\"" + licenceNo + "\",\"carVIN\":\"\",\"plateColor\":\"1\"}}}";

  



    StringBuffer jsonString;

    try {

        URL url = new URL(requestUrl);

        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoInput(true);

        connection.setDoOutput(true);

        connection.setRequestMethod("POST");

        connection.setRequestProperty("Accept", "application/json");

        connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");

        connection.setRequestProperty("Cookie", cookStr);

        connection.setRequestProperty("Host", host);

        connection.setRequestProperty("Referer", referer);


        OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

        writer.write(payload);

        writer.close();

        BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

        jsonString = new StringBuffer();

        String line;

        while ((line = br.readLine()) != null) {

            jsonString.append(line);

        }

        br.close();

        connection.disconnect();

    } catch (Exception e) {

        throw new RuntimeException(e.getMessage());

    }

    return jsonString.toString();

}


发表评论:

Powered By Z-BlogPHP 1.4 Deeplue Build 150101

Copyright Your WebSite.Some Rights Reserved.

蜀ICP备11021721号-5