我正在尝试将发布请求发送到具有电子邮件和密码的网络服务。当我在参数(即 qadir.suh@gmail.com)中添加特殊字符 @ 时,它会转换为 %40。我检查了服务器端,他们得到的是 %40 而不是 @。
这是我的代码:
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://www.myurl.com/requesthandler.ashx");
// from list to arraylist
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
3);
nameValuePairs.add(new BasicNameValuePair("txtUserId",
"qadir.suh@gmail.com"));
nameValuePairs.add(new BasicNameValuePair("txtPassword",
"blahblah"));
nameValuePairs.add(new BasicNameValuePair("type", "login"));
try {
httppost.setEntity(new UrlEncodedFormEntity(
nameValuePairs));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Execute HTTP Post Request
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("Client Protocol Excption", e + "");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("IO Exception", e + "");
}
String responseText = null;
try {
responseText = EntityUtils.toString(response
.getEntity());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("Parse Exception", e + "");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.i("IO Exception 2", e + "");
}
String afterDecode = null;
try {
afterDecode = URLDecoder.decode(responseText, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(MainActivity.this, afterDecode,
Toast.LENGTH_LONG).show();
我知道
httppost.setEntity(new UrlEncodedFormEntity(
nameValuePairs));
这会将 URL 编码为 UTF-8。那么我怎样才能实现我的目标,以便服务器应该接收 @ 符号而不是 %40?或者有什么方法可以在不使用的情况下发布请求setEntity(new UrlEncodedFormEntity(nameValuePairs))
?或者有什么方法可以让我们发送 POST 请求的解码版本?