如何在执行 apache HttpPost 时将 UrlEncodedFormEntity 中的空格编码为 %20?
我正在访问的网络服务需要 URLEncodedFormEntity 形式的参数。我无法根据 Web 服务的要求将空间更改为 %20,而是将空间转换为 +。
我的代码是:
HttpClient client = new DefaultHttpClient()
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
HTTP.UTF_8);
post.setEntity(entity);
HttpResponse resp = client.execute(post);
其中参数是 List
参数。
我读了很多帖子,都建议在编码后手动将空间更改为 %20。在这里,我如何访问实体并手动更改它? 任何帮助将不胜感激。
The web serive i am hitting requires the parameters as URLEncodedFormEntity. I am unable to change space to %20 as per requirement of the web service, instead space is converted to +.
My code is :
HttpClient client = new DefaultHttpClient()
HttpPost post = new HttpPost(url);
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
HTTP.UTF_8);
post.setEntity(entity);
HttpResponse resp = client.execute(post);
where parameters is List<NameValuePair>
parameters.
I read through many posts and all suggest manuall change space to %20 after emcoding. Here, how do i access the entity and change it manually?
Any help will be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UrlEncodedFormEntity 基本上是一个带有自定义构造函数的 StringEntity,您实际上不必使用它来创建可用的实体。
The UrlEncodedFormEntity is basically a StringEntity with a custom constructor, you don't actually have to use it in order to create a usuable entity.
Jens 的回答很有魅力!为了完成他的示例,我用它来发布参数:
但它总是发布“+”字符串,我的意思是“label=A+label”。使用 Jens 的建议,我将代码更改为:
现在它发布“label=A%20label”
Jens' answer works like a charm!. To complete his example this what I was using to post a parameter:
But it always posts the "+" string, I mean, "label=A+label". Using Jens' suggestion I changed my code to:
Now it posts "label=A%20label"