内容长度不打印

时间:2016-07-10 11:57:21

标签: java httpurlconnection

我试图打印所有标题以检查我的请求,但不幸的是它会打印所有标题,期望内容长度为什么它不打印内容长度?问题是什么?这是我的要求

public static String performPostCall(String requestURL,
        String postDataParams, int length, PrintWriter pw) throws Exception {
    URL url;
    String response = "";
    try {
        url = new URL(requestURL);

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(10000);
        conn.setRequestMethod("POST");

        conn.setRequestProperty("SOAPAction",
                "http://tempuri.org/SendRequest");
        conn.setRequestProperty(HTTP.CONTENT_TYPE,
                "text/xml; charset=utf-8");
        conn.setRequestProperty("Content-Length", String.valueOf(length));

        conn.setDoInput(true);
        conn.setDoOutput(true);
        pw.println("Post request: " + url);
        for (String header : conn.getRequestProperties().keySet()) {
            if (header != null) {
                for (String value : conn.getRequestProperties().get(header)) {
                    System.out.println(header + ":" + value);
                }
            }
        }
        pw.println(postDataParams);
    //code..

    return response;
}

1 个答案:

答案 0 :(得分:1)

我认为 Content-Length 是一个标题属性,会根据您的请求大小自动设置。