问题描述
我有一个端点,要求使用格式如下的"authenticity_token":
I have an endpoint that requires an 'authenticity_token' that is in the format like:
Iq2rNXN+OxERv+s6TSloJfKkPZVvqnWe1m0NfODB5OI=
但是,有时它具有特殊"字符,例如:
However, sometimes it has "special" characters, such as:
E7IzeP73OgPGgXM/up295ky1mMQMio2Nb8HMLxJFyfw=
这被编码为:
E7IzeP73OgPGgXM%26%2347%3Bup295ky1mMQMio2Nb8HMLxJFyfw%3D
由于某种原因,端点不喜欢这些特殊字符的编码,并且会认为令牌无效.是否可以添加不编码特定值的POST变量?我目前正在做类似的事情:
For some reason, the endpoint does not like the encoding of those special characters and will think the token is invalid. Is it possible to add a POST variable that does not encode specific values? I am currently doing something like:
HttpPost post = new HttpPost(URL + NEW_FINDING);
List <NameValuePair> nvps = new ArrayList <NameValuePair>();
nvps.add(new BasicNameValuePair("foo", foo));
nvps.add(new BasicNameValuePair("authenticity_token", authenticityToken));
post.setEntity(new UrlEncodedFormEntity(nvps));
推荐答案
您始终可以使用ByteArrayEntity
或StringEntity
代替UrlEncodedFormEntity
并自己进行编码.它看起来应该像foo=var1&bar=var2
.
您必须设置Content-Type=application/x-www-form-urlencoded
您可能想要找出您的终端期望作为Content-Type标头的application/x-www-form-urlencoded
值的字符集参数.然后将其作为参数传递给UrlEncodedFormEntity
构造函数.这应该是正确的解决方法.
You can always use ByteArrayEntity
or StringEntity
instead of UrlEncodedFormEntity
and do the encoding yourself. It should look something like foo=var1&bar=var2
.
You have to set Content-Type=application/x-www-form-urlencoded
You may want to find out what your endpoint expects as a charset parameter for the application/x-www-form-urlencoded
value of the Content-Type header. Then pass it as a parameter to the UrlEncodedFormEntity
constructor. This should be the right fix.
这篇关于Java Apache HTTPClient不对POST实体进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!