0

我正在使用 Apache HttpComponents,由于某种原因,当我尝试使用查询参数执行 HttpPost 时,uri 不包含参数。

这是一段代码摘录:

List<BasicNameValuePair> parametersBody = new ArrayList<BasicNameValuePair>();

parametersBody.add(new BasicNameValuePair("response_type", "code"));

// continue adding parameters...

HttpPost post = new HttpPost("https://www.fitbit.com/oauth2/authorize");
post.setEntity(new UrlEncodedFormEntity(parametersBody, StandardCharsets.UTF_8));

HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(post);

当我使用 EntityUtils.toString(post.getEntity()) 检查帖子的参数时,所有参数都按预期存在......但是当我执行帖子时,它正在导航到“ https://www.fitbit.com/oauth2 /授权?空

我需要在这里做什么?如果它有所作为,这不适用于 Android 应用程序。谢谢你的帮助。

编辑:目前,我正在做这样的解决方法:

String uri = "https://www.fitbit.com/oauth2/authorize?"
             + EntityUtils.toString(new UrlEncodedFormEntity(parametersBody, StandardCharsets.UTF_8);
HttpPost post = new HttpPost(uri);
client.execute(post);
4

1 回答 1

0

请求他们的支持。我的猜测是他们的服务器已损坏,无法理解您的 UrlEncodedFormEntity 正在生成的带有“Content-Type: application/x-www-form-urlencoded; charset=UTF-8”的 POST 请求。

他们的 API 示例显示了带有简单“Content-Type: application/x-www-form-urlencoded”的请求(由许多浏览器生成)。

尝试显式设置内容类型,有一个setter方法

post.getEntity().setContentType("application/x-www-form-urlencoded")
于 2015-11-12T20:15:05.683 回答