java - Android 4.0 将 POST 变成 GET

标签 java android httpurlconnection android-4.0-ice-cream-sandwich

我有一个要移植到 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/

相关文章:

android - 重新签署 Android Apk

java - 使用 HttpURLConnection 获取远程 url 内容时如何保留换行符和空行?

java - 如何使用 Project Reactor 重复或重试动态值?

java - 尝试在单击 J 按钮时来回更改其颜色

java - Eclipse 总是复制大量资源

java.io.IOException : Cannot run program ".\bin\catalina.bat" - CreateProcess error=2, 系统找不到指定的文件

android - 分 ionic 项、更新并附加?安卓引擎

android - 如何在第二个java文件中显示 toast ?

java - 执行 PUT 请求时出现 HTTP 403

httpurlconnection - 使用 "full message"http 响应时如何确保已读取 "Transfer-Encoding:chunked"