如何在执行 apache HttpPost 时将 UrlEncodedFormEntity 中的空格编码为 %20?

发布于 2024-12-11 22:48:23 字数 495 浏览 0 评论 0原文

我正在访问的网络服务需要 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

中二柚 2024-12-18 22:48:23

UrlEncodedFormEntity 基本上是一个带有自定义构造函数的 StringEntity,您实际上不必使用它来创建可用的实体。

String entityValue = URLEncodedUtils.format(parameters, HTTP.UTF_8);
// Do your replacement here in entityValue
StringEntity entity = new StringEntity(entityValue, HTTP.UTF_8);
entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
// And now do your posting of this entity

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.

String entityValue = URLEncodedUtils.format(parameters, HTTP.UTF_8);
// Do your replacement here in entityValue
StringEntity entity = new StringEntity(entityValue, HTTP.UTF_8);
entity.setContentType(URLEncodedUtils.CONTENT_TYPE);
// And now do your posting of this entity
对你再特殊 2024-12-18 22:48:23

Jens 的回答很有魅力!为了完成他的示例,我用它来发布参数:

String label = "A label";

List<NameValuePair> nvps = new ArrayList<NameValuePair>();

nvps.add(new BasicNameValuePair("label", label));
httpget.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

但它总是发布“+”字符串,我的意思是“label=A+label”。使用 Jens 的建议,我将代码更改为:

String label = "A label";

List<NameValuePair> nvps = new ArrayList<NameValuePair>();

nvps.add(new BasicNameValuePair("label", label));

String entityValue = URLEncodedUtils.format(nvps, HTTP.UTF_8);
entityValue = entityValue.replaceAll("\\+", "%20");

StringEntity stringEntity = new StringEntity(entityValue, HTTP.UTF_8);
stringEntity.setContentType(URLEncodedUtils.CONTENT_TYPE);

httpget.setEntity(stringEntity);

现在它发布“label=A%20label”

Jens' answer works like a charm!. To complete his example this what I was using to post a parameter:

String label = "A label";

List<NameValuePair> nvps = new ArrayList<NameValuePair>();

nvps.add(new BasicNameValuePair("label", label));
httpget.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

But it always posts the "+" string, I mean, "label=A+label". Using Jens' suggestion I changed my code to:

String label = "A label";

List<NameValuePair> nvps = new ArrayList<NameValuePair>();

nvps.add(new BasicNameValuePair("label", label));

String entityValue = URLEncodedUtils.format(nvps, HTTP.UTF_8);
entityValue = entityValue.replaceAll("\\+", "%20");

StringEntity stringEntity = new StringEntity(entityValue, HTTP.UTF_8);
stringEntity.setContentType(URLEncodedUtils.CONTENT_TYPE);

httpget.setEntity(stringEntity);

Now it posts "label=A%20label"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文