Angel
Angel

Reputation: 199

HTTP POST request in java - setting content length

Below is my code which does POST request to login to a website. When i run the same I receive Message response as 411. Can anybody help me to set the correct Content Length?

  try {

        String request = "http://<domain.com>/login";

        URL url = new URL(request);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);

        connection.setInstanceFollowRedirects(false);
        connection.setRequestProperty("Content-Length", "1");
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

        connection.setRequestProperty("email", "abc");
        connection.setRequestProperty("password", "xyz");

        connection.setDoOutput(true);

        connection.setRequestMethod("POST");

        int code = connection.getResponseCode();
        System.out.println("Response Code of the object is " +code);
        if(code == 200)
            System.out.println("OK");
        connection.disconnect();
        } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Upvotes: 2

Views: 11809

Answers (3)

Nikos Kyr
Nikos Kyr

Reputation: 3246

Although the answer might be too late, but just for future reference. I faced exactly the same problem and for the reasons already mentioned I kept getting the same error.

I managed to work around the problem by sending just an empty body:

  conn.setDoOutput(true);
    try (OutputStream os = conn.getOutputStream()) {
        byte[] input = "".getBytes("utf-8");
        os.write(input, 0, input.length);
    }

That fixed the issue for me. Manually setting the Content-Length header does not help as already mentioned.

Upvotes: 2

Wang Zhong
Wang Zhong

Reputation: 190

I was facing the same problem. I was trying to send a POST request with an empty body, but the request was rejected by the server, returning 411.

Then I found the following code in 'sun.net.www.protocol.http.HttpURLConnection', which is the implementation of 'java.net.HttpURLConnection':

  private static final String[] restrictedHeaders = new String[]{"Access-Control-Request-Headers", "Access-Control-Request-Method", "Connection", "Content-Length", "Content-Transfer-Encoding", "Host", "Keep-Alive", "Origin", "Trailer", "Transfer-Encoding", "Upgrade", "Via"};

So, when you set a header via sun.net.www.protocol.http.HttpURLConnection.setRequestProperty, if the key is included in the array above, it will be ignored. That's why setting 'Content-Length' manually is not working.

Upvotes: 0

BillFromHawaii
BillFromHawaii

Reputation: 334

Ok - you're not moving anything into the body of the request so you should set the length to "0".

Add:

connection.connect();

after:

connection.setRequestMethod("POST");

google "httpurlconnection post parameters" for adding parameters to a POST request. This site has at least four or five solutions.

Upvotes: 2

Related Questions