我在网站上发出简单的 HTTP POST 请求时遇到了一些麻烦。好像没有发送 POST 参数!
场景:
HTTP 响应代码 200 - 错误的用户/密码 - 发生在 Android 4.0+ 上
HTTP 响应代码 302 - 用户/密码 OK - 发生在其他 SDK 版本上
可以正常工作的 Web 表单:
<form id="form_mn_login" enctype="application/x-www-form-urlencoded" action="http://dummyhost/r.php" class="estil" accept-charset="utf-8" method="post">
<input type="text" name="m_email" id="m_email" />
<input type="password" name="m_senha" id="m_senha" />
代码(在 doInBackground AsyncTask 的方法中):
urlParameters = "m_email="
+ URLEncoder.encode(username, "UTF-8") + "&m_senha="
+ URLEncoder.encode(password, "UTF-8");
URL url;
HttpURLConnection connection = null;
// Create connection
url = new URL("http://dummyhost/r.php");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Language", "en-US");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
// Send request
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
publishProgress(30);
Log.w("responsecode", Integer.toString(connection.getResponseCode()));
此问题仅在我在 Android 4.0+ AVD 设备上模拟时出现。(在 2.2、2.3.3、3.0 上试过,它完美无缺)我错过了什么?
我知道这可能是服务器问题,但我真的相信必须有一个解决方法。我已经尝试了数千种不同的方法,但似乎没有一种方法可以正常工作。
非常感谢,RC