1

我正在尝试将发布请求发送到具有电子邮件和密码的网络服务。当我在参数(即 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 请求的解码版本?

4

1 回答 1

0

您正在发布 URL 编码的表单参数,但可能没有将 content-type 设置为"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));
}
于 2012-11-13T10:51:55.593 回答