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