2

I have a Gingerbread Android application that I'm porting to ICS. This application communicates with a web server sending HTTP POST. My application runs fine on Gingerbread. However, I have been experiencing problems after porting it to ICS. I found out that the POST requests my application is sending are actually changed to GET.

The funny thing is, Android actually reports that POST is indeed used.

URL oURL = new URL(sURL);

HttpURLConnection oHTTPConnection = (HttpURLConnection)(oURL.openConnection());
oHTTPConnection.setDoInput(true);
oHTTPConnection.setDoOutput(true);
oHTTPConnection.setRequestMethod("POST");

// set headers...
int nResponse = oHTTPConnection.getResponseCode();

String sMethod = oHTTPConnection.getRequestMethod();  // Returns "POST"

However, the server would say otherwise. I modified the web server application to check the request method it receives and then put this value in the response body it sends back to my Android application. And what I receive on my Android application is "GET".

I have tried using HttpClient with HttpPost but I get the same issue.

As I mentioned, I didn't have this problem in Gingerbread. Also, I've read from another thread here a similar (but opposite) problem that also only happens in ICS: Android 4.0 ICS turning HttpURLConnection GET requests into POST requests.

Has anyone else experienced this? Can anyone help me solve this?

Thanks in advance!

Rai

Community
  • 1
  • 1
rainai
  • 468
  • 4
  • 8

2 Answers2

1

Try follow this answer: https://stackoverflow.com/a/8799198/372076

I've found that pre-ICS one could get away with making a body-less POST without providing a Content-Length value, however post-ICS you must set Content-Length: 0.

Community
  • 1
  • 1
Pedro Loureiro
  • 11,436
  • 2
  • 31
  • 37
  • Thanks but didn't work either. I removed some parts of the code I posted above, but, in fact, I already have set Content-Length to 0. I have been doing this since my Gingerbread version. – rainai Feb 21 '12 at 02:24
0

Don't know if you already found a fix for this, but i was having the same problem and just found a work around. In my case it was an issue on server's side with Apache's redirection. I was doing:

Url url = new Url("http://aaaa.bbbb.com/");

Changed to:

Url url = new Url("http://aaaa.bbbb.com/index.php");

Somehow the redirection was turning my POST into a GET with no parameters.

DSLima90
  • 2,680
  • 1
  • 15
  • 23
  • Redirection works by sending a header "Location: new url", and the http client follows this url using GET, regardless of the original request method. So it is not an issue of the server. – hammady Mar 29 '18 at 09:47
  • It can also happen when you POST a request to an http endpoint, and the server redirects to the https equivalent. – hammady Mar 29 '18 at 09:47