由于密码中的特殊字符而无法发送POST请求

时间:2014-07-15 14:52:33

标签: escaping apache-httpclient-4.x

所以当我用

发帖子请求时
httpClient.execute(httpPost)

我得到以下输出:

09:09:12.181 [main] DEBUG org.apache.http.wire - http-outgoing-0 >> "username=qliu&password=213%40vPi" # <==== THIS LINE
    09:09:12.283 [main] DEBUG org.apache.http.wire - http-outgoing-0 << "HTTP/1.1 200 OK[\r][\n]"

我的密码实际上是213@vPi而不是213%40vPi但我不知道如何逃避&#34; @&#34;符号

String password = "213@vPi"; // TODO: how to escape @

1 个答案:

答案 0 :(得分:0)

使用StringEntity类来代替。

HttpPost prepareLoginHttpPost() {
        HttpPost httpPost = new HttpPost(Configurations.SAND_LOGIN_URL);

        StringEntity authorizationInJSON_Entity = new StringEntity(Configurations.authorizationInJSON);

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

        return httpPost;
    }