【问题标题】:Apache HttpPost isn't sending the query parameters?Apache HttpPost 没有发送查询参数?
【发布时间】:2015-11-12 18:45:06
【问题描述】:

我正在使用 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/authorize?null

我需要在这里做什么?如果它有所作为,这不适用于 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);

【问题讨论】:

    标签: java oauth-2.0 apache-httpcomponents fitbit


    【解决方案1】:

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

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

    尝试显式设置content-type,有setter方法

    post.getEntity().setContentType("application/x-www-form-urlencoded")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 2017-11-01
      • 2012-01-31
      • 1970-01-01
      相关资源
      最近更新 更多