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

}

发布了71 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_35577329/article/details/97786854
今日推荐