【问题标题】:@ is converted into %40 at server side@ 在服务器端转换为 %40
【发布时间】: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


【解决方案1】:

您发布的是 URL 编码的表单参数,但可能没有将内容类型设置为 "x-www-form-urlencoded"。这是必需的,以便服务器知道如何解释发布的参数。尝试在调用httppost.setEntity()之前添加此行:

httppost.addHeader("Content-Type","application/x-www-form-urlencoded");

编辑:如果您不想使用 URL 编码,您可以简单地发布一个字符串

如果您不想对 POST 正文进行 URL 编码,则可以这样做:

// Create post body as name/value pairs in form format
String postBody = "txtUserId=qadir.suh@gmail.com&txtPassword=blahblah&type=login";
try {
    httppost.setEntity(new StringEntity(postBody));
}

【讨论】:

  • 感谢大卫的回答。我也试过这个,但服务器仍然得到 %40 而不是 @ 符号。那有什么问题..:(
  • 是的,您将始终获得 %40 而不是“@”,因为您正在创建一个 URL 编码的 POST 正文。但是,使用正确的标头,服务器将能够正确地将 %40 转换为“@”。如果可能的话,看看你是否能找出发送到服务器的 POST 请求的完整内容。您想查看所有 HTTP 标头。
  • 好吧,你想说的是,先解码然后发送到服务器端并不是我的头疼.. ??
  • 是的,我的意思是如果您正确发送它,您的服务器应该能够解码它。我刚刚编辑了我的答案,向您展示了另一种不使用 URL 编码的方法。这将起作用,但前提是 POST 数据不包含任何在 URL 编码形式中非法的字符(如“&”或“=”)
  • 这工作大卫.. 非常感谢您的合作。我想问一件事,像你在没有编码的情况下向服务器发送请求有什么缺点吗? (即 String postBody = "txtUserId=qadir.suh@gmail.com&txtPassword=blahblah&type=login"; 尝试 { httppost.setEntity(new StringEntity(postBody)); } )
猜你喜欢
  • 1970-01-01
  • 2011-12-17
  • 2012-08-30
  • 2013-03-05
  • 1970-01-01
  • 2014-09-05
  • 1970-01-01
  • 2013-12-19
  • 2016-12-30
相关资源
最近更新 更多