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

标签 android apache encoding httpclient url-encoding

我正在访问的网络服务需要参数作为 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<NameValuePair>参数。

我通读了很多帖子,都建议在编码后手动将空间更改为 %20。在这里,我如何访问实体并手动更改它? 任何帮助将不胜感激。

最佳答案

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

关于android - 执行apache HttpPost时如何在UrlEncodedFormEntity中将空格编码为%20?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7915029/

相关文章:

apache - Storm ui错误:org.apache.storm.utils.NimbusLeaderNotFoundException:从种子主机[localhost]中找不到领导者雨云

encoding - 如何设置WildFly 9.0.1.Final httpUri编码

java - 使用 SDK 12 和兼容性库的 Eclipse 导出签名 APK 失败

android - 实现 Geocoder 时返回 null

regex - 如何将 www 放在 htaccess 中的 wordpress 站点 url 前面?

apache - 设置 apache2 以执行 cgi 脚本

android - 木材 Lint : ClassNotFound: com/android/tools/lint/detector/api/Detector$JavaPsiScanner

android - 没有任何代码更改的 Android Studio 的 Gradle 构建失败

php - 为什么默认情况下 PHP 字符串函数不是多字节安全的?

Python 2.7 编码和 feedparser