3

我是 Java 新手。我编写了一个带有 gui 的小程序,将结果(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());
    }

}
4

3 回答 3

1

我不是 100% 确定您为什么会收到 411 错误代码,但这可能与您没有通过 POST 发送任何内容这一事实有关。content-length 标头应该是请求正文的字节长度。您正在将其设置为 url 的长度!

要么将请求更改为 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());
    }

}
于 2012-12-14T04:54:46.410 回答
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());
    }

}
于 2012-12-14T04:59:47.857 回答
0

由于所有信息都作为 URL 的一部分作为查询字符串传递,因此您无需 setRequestMethod设置为POST。但是,如果您仍想将其设置为 POST 作为逻辑的一部分,那么setRequestProperty("Content-Length", "0") as Http Server 期望在调用 POST 方法时读取 Length。在您的情况下,没有任何内容被写入 POST 信息,因此最好将其设置为零,即使有时默认值为零,服务器可能会给出响应错误代码为 411,响应消息:所需长度

于 2016-03-23T15:04:51.783 回答