Android
Android

Reputation: 1489

issue in httpurlconnection getting status 500

I am trying to login through url and i am getting status code 500 in httpurlconnevtion

public static String excutePost(String targetURL, String urlParameters)
  {
    URL url;
    HttpURLConnection connection = null;  
    try {
      //Create connection
      url = new URL(targetURL);
      connection = (HttpURLConnection)url.openConnection();
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Content-Type", 
           "application/x-www-form-urlencoded");
      connection.setRequestProperty("Connection", "Keep-Alive");    
      connection.setRequestProperty("Content-Length", "" + 
               Integer.toString(urlParameters.getBytes().length));
      connection.setRequestProperty("Content-Language", "en-US");  
      connection.setRequestProperty("Accept-Encoding", ""); 
      connection.setUseCaches (false);
      connection.setDoInput(true);
      connection.setDoOutput(true);

      //Send request
      DataOutputStream wr = new DataOutputStream (
                  connection.getOutputStream ());
      wr.writeBytes (urlParameters);
      wr.flush ();
      wr.close ();

      System.out.println("status :"+connection.getResponseCode());
      System.out.println("getErrorStream() :"+connection.getErrorStream());
      //Get Response    
      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
      StringBuffer response = new StringBuffer(); 
      while((line = rd.readLine()) != null) {
        response.append(line);
        response.append('\r');
      }
      rd.close();
      return response.toString();

    } catch (Exception e) {

      e.printStackTrace();
      return null;

    } finally {

      if(connection != null) {
        connection.disconnect(); 
      }
    }
  }

and my params are

String urlParameters =
                        "pwd1=" + URLEncoder.encode("DEMO123", "UTF-8") +
                        "&badge=" + URLEncoder.encode("1233", "UTF-8");

i am getting logcat

status :500
getErrorStream() :libcore.net.http.FixedLengthInputStream@417bc5c0

thank you

**EDITED 2**

I have also try with

DataOutputStream dos = new DataOutputStream(connection.getOutputStream());

        // Add badge
        dos.writeBytes(LINE_START + BOUNDRY + LINE_END);
        dos.writeBytes("Content-Disposition: form-data; name='badge';");
        dos.writeBytes(LINE_END + LINE_END);
        dos.writeBytes("1233");
        dos.writeBytes(LINE_END);

        // Add password
        dos.writeBytes(LINE_START + BOUNDRY + LINE_END);
        dos.writeBytes("Content-Disposition: form-data; name='pwd1';");
        dos.writeBytes(LINE_END + LINE_END);
        dos.writeBytes("DEMO123");
        dos.writeBytes(LINE_END);

Upvotes: 1

Views: 7827

Answers (2)

Sulthan
Sulthan

Reputation: 130200

500 denotes an Internal Server Error. There is probably no error on your side, it's on the server. Even if you are sending something incorrectly and it's causing the server to return 500, it's still a server problem.

Edit: Okey, the server should rather return something like 400 Bad Request instead of 500 Internal Server Error but I found your error now:

connection.setRequestProperty("Content-Length", Integer.toString(urlParameters.getBytes().length));

...

//Send request
DataOutputStream wr = new DataOutputStream (connection.getOutputStream ());
wr.writeBytes (urlParameters);

The problem here is that your first get the the bytes from urlParameters using getBytes which (quoting javadoc):

Encodes this String into a sequence of bytes using the platform's default charset

And then you write the bytes using DataOutputStream.writeBytes which (quoting javadoc):

Each character in the string is written out, in sequence, by discarding its high eight bits.

In summary, your Content-Length doesn't match the data. So the server returns you the

java.io.IOException: exceeded content-length limit of 20 bytes

Solution:

//consider urlParameters.getBytes("UTF-8") method instead of using default encoding
byte[] bodyData = urlParameters.getBytes(); 
connection.setRequestProperty("Content-Length", Integer.toString(bodyData.length));

...

//Send request
InputStream out = connection.getOutputStream();
out.write(bodyData);

Edit 2: Edit 1 is definitely valid, however, looking over the problem again, I believe the error is definitely caused by the server. I think the server is returning a bad Content-Length header and, when the data is read on Android, the system realizes there is more data coming from the server than it should be by the Content-Length and throws an exception, also replacing the status code by 500 because it really is a server error.

Edit 3:

connection.setRequestProperty("Content-Language", "en-US");  
connection.setRequestProperty("Accept-Encoding", ""); 

Instead of setting Content-Language which is not neccessary here, you should set Content-Encoding to UTF-8 and instead of empty Accept-Encoding, you should add the real expected MIME-type. I believe this is a server error, but you maybe it won't appear if you set the request headers correctly.

Upvotes: 1

salezica
salezica

Reputation: 77109

Status code 500 means Internal Server Error. Why this is thrown at you, only the server behind targetURL knows.

Verify that you're making correct usage of the API. Taking a look at the response's body (besides the status code) may provide a hint.

Upvotes: 0

Related Questions