Java Applet 411内容长度

时间:2012-12-14 04:49:11

标签: java applet japplet content-length http-content-length

我是Java新手。我写了一个带有gui的applet,它将结果(int w和int p)发送到服务器,我得到“411 Length Required”错误。我究竟做错了什么?你如何设置内容长度?

这是与服务器通信的方法:

public void sendPoints1(int w, int p){

    try {
        String url = "http://somename.com:309/api/Results";
        String charset = "UTF-8";
        String query = String.format("?key=%s&value=%s",
            URLEncoder.encode(String.valueOf(w), charset),
            URLEncoder.encode(String.valueOf(p), charset));
        String length = String.valueOf((url + query).getBytes("UTF-8").length);

        HttpURLConnection connection = (HttpURLConnection) new URL(url + query).openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Length", length);
        connection.connect();
        System.out.println("Responce Code:    " + connection.getResponseCode());
        System.out.println("Responce Message: " + connection.getResponseMessage());
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

}

3 个答案:

答案 0 :(得分:1)

我不是100%肯定你为什么会收到411错误代码,但这可能与你没有发送任何内容的事实有关。 content-length头应该是请求主体的字节长度。您将其设置为网址的长度!

将请求更改为GET或将查询放入请求正文而不是URL本身。如果您执行后者,请将content-length设置为正文的长度。

public void sendPoints1(int w, int p){

    try {
        String url = "http://somename.com:309/api/Results";
        String charset = "UTF-8";
        String query = String.format("key=%s&value=%s",
            URLEncoder.encode(String.valueOf(w), charset),
            URLEncoder.encode(String.valueOf(p), charset));
        byte[] queryBytes = query.getBytes("UTF-8");
        String length = String.valueOf((url + query).getBytes("UTF-8").length);

        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Length", length);
        OutputStream os = connection.getOutputStream();
        os.write(queryBytes);
        os.flush();
        connection.connect();
        System.out.println("Responce Code:    " + connection.getResponseCode());
        System.out.println("Responce Message: " + connection.getResponseMessage());
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

}

答案 1 :(得分:0)

请求uri包含查询,可以使用GET方法。

connection.setRequestMethod("POST"); // modify to GET
connection.setRequestProperty("Content-Length", length); //remove the line

如果使用POST方法,包含'content-length'标题,必须发送数据。

例如:

public void sendPoints1(int w, int p){

    try {
        String url = "http://somename.com:309/api/Results";
        //value type is int ,don't need URLEncoder.
        byte[] data = ("key="+w+"&value="+p).getBytes("UTF-8");

        HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Length", data.length);
 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //URLencode...
        OuptputStream out = connection.getOutputStream();
        out.write(data);
        out.flush();
        InputStream in = connnection.getInputStream();
        //read .....
        System.out.println("Responce Code:    " + connection.getResponseCode());
        System.out.println("Responce Message: " + connection.getResponseMessage());
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }

}

答案 2 :(得分:0)

由于所有信息都作为URL的一部分作为查询字符串传递,因此您需要 setRequestMethod 作为 POST 。但是如果您仍然希望将其设置为POST作为逻辑的一部分,则 setRequestProperty(“Content-Length”,“0”),因为Http Server期望在调用POST方法时读取Length。在你的情况下,没有任何内容被写为POST信息,所以最好将其设置为ZERO,甚至认为默认值为ZERO有时服务器可能会给出响应errorCode为411,响应消息:所需长度