我有一个要移植到 ICS 的 Gingerbread Android 应用程序。此应用程序与发送 HTTP POST 的 Web 服务器通信。我的应用程序在 Gingerbread 上运行良好。但是,移植到 ICS 后,我一直遇到问题。我发现我的应用程序发送的 POST 请求实际上已更改为 GET。
有趣的是,Android 实际上报告确实使用了 POST。
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"
但是,服务器会另有说明。我修改了 Web 服务器应用程序以检查它接收到的请求方法,然后将此值放入它发送回我的 Android 应用程序的响应正文中。我在我的 Android 应用程序上收到的是“GET”。
我试过将 HttpClient 与 HttpPost 一起使用,但我遇到了同样的问题。
正如我提到的,我在 Gingerbread 中没有遇到这个问题。另外,我在这里的另一个线程中读到一个类似(但相反)的问题,它也只发生在 ICS 中:Android 4.0 ICS turning HttpURLConnection GET requests into POST requests .
有没有人遇到过这种情况?谁能帮我解决这个问题?
提前致谢!
赖
最佳答案
试着按照这个答案: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.
关于java - Android 4.0 将 POST 变成 GET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9359750/