【发布时间】:2012-11-13 06:53:09
【问题描述】:
我正在尝试将发布请求发送到具有电子邮件和密码的网络服务。当我在参数中添加特殊字符 @(即 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 请求?
【问题讨论】:
-
在服务器端添加一个代码来解码 post 参数。
-
我们为什么要这样做,先生..!我是一名安卓开发者。基本上我还没有开发网络服务。我只是提供了一个网址,我没有机会更改网络服务。
-
问题出在服务器上。这是正常的 URL 编码。 en.wikipedia.org/wiki/Percent-encoding
-
我还要说的一件事是我在 Iphone 上测试过这个。并且也来自 ASP.net 表单,它工作正常。
-
URL 只能使用 ASCII 字符集通过 Internet 发送。此集之外的任何字符都将转换为 ASCII 格式,以便可以通过 http 进行设置。你不能按原样发送。您必须在服务器端添加一个 url 解码代码。
标签: android encoding urlencode encode