Java Applet 411 Content Length

Posted by user1903006 on Stack Overflow See other posts from Stack Overflow or by user1903006
Published on 2012-12-14T04:49:11Z Indexed on 2012/12/14 5:03 UTC
Read the original article Hit count: 193

I am new to Java. I wrote an applet with a gui that sends results (int w and int p) to a server, and I get the "411 Length Required" error. What am I doing wrong? How do you set a Content-Length?

This is the method that communicates with the server:

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

}

© Stack Overflow or respective owner

Related posts about java

Related posts about applet