无法更改发出请求的帖子请求

时间:2014-12-12 17:51:11

标签: java http

我为HTTP-Request编写了一个类。 我的问题是,尽管在这段代码的几行中我添加了:

httpConn.setRequestMethod("PUT");

我的代码仍然执行POST-Request而不是PUT-Request!我错了什么?谢谢!

public MultipartUtility(String requestURL, String charset)
            throws IOException {
        this.charset = charset;

        // creates a unique boundary based on time stamp
        boundary = "===" + System.currentTimeMillis() + "===";

        URL url = new URL(requestURL);
        httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setRequestMethod("PUT");
        httpConn.setDoOutput(true); // indicates POST method
        httpConn.setRequestMethod("PUT");
        httpConn.setDoInput(true);

        httpConn.setRequestProperty("Content-Type",
                "multipart/form-data; boundary=" + boundary);
        httpConn.setRequestProperty("User-Agent", "CodeJava Agent");
        httpConn.setRequestProperty("Test", "Bonjour");
        outputStream = httpConn.getOutputStream();
        writer = new PrintWriter(new OutputStreamWriter(outputStream, charset),
                true);
    }

1 个答案:

答案 0 :(得分:1)

httpConn.setUseCaches(false);可以影响此结果。我不确定PUT是否可以提供此功能