@在服务器端转换为%40

时间:2012-11-13 06:53:10

标签: android encoding urlencode encode

我正在尝试向具有电子邮件和密码的网络服务发送帖子请求。当我在参数(即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));

这会以UTF-8编码URL。那么我怎样才能实现我的目标,以便服务器应该接收@符号而不是%40?或者有没有方法在不使用setEntity(new UrlEncodedFormEntity(nameValuePairs))的情况下发布请求?或者是否有任何方法可以发送POST请求的解码版本?

1 个答案:

答案 0 :(得分:0)

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

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

编辑:如果您不想使用网址编码,只需发布​​一个字符串

如果您不想对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));
}