简体   繁体   English

Apache HttpPost不发送查询参数吗?

[英]Apache HttpPost isn't sending the query parameters?

I'm using Apache HttpComponents and for some reason when I'm trying to execute an HttpPost with query params, the uri isn't including the parameters. 我使用的是Apache HttpComponents,由于某种原因,当我尝试使用查询参数执行HttpPost时,uri不包含参数。

Here's a code excerpt: 这是一段代码摘录:

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);

When I check the post's params with EntityUtils.toString(post.getEntity()), all of the parameters are there as expected... however when I execute the post it's navigating to " https://www.fitbit.com/oauth2/authorize?null " 当我使用EntityUtils.toString(post.getEntity())检查帖子的参数时,所有参数都如预期的那样...但是,当我执行帖子时,它导航到“ https://www.fitbit.com/oauth2 / authorize?null

What do I need to do here? 我在这里需要做什么? If it makes a difference, this is not for an Android app. 如果有所作为,则不适用于Android应用程序。 Thanks for any help. 谢谢你的帮助。

Edit: For the time being, I'm doing a workaround like so: 编辑:目前,我正在做这样的一种解决方法:

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

Ask their support. 寻求他们的支持。 My guess is that their server is broken and cannot understand POST requests with "Content-Type: application/x-www-form-urlencoded; charset=UTF-8" that your UrlEncodedFormEntity is producing. 我的猜测是,他们的服务器已损坏,并且无法理解您的UrlEncodedFormEntity正在生成的“内容类型:application / x-www-form-urlencoded; charset = UTF-8”的POST请求。

Their API examples show requests with simple "Content-Type: application/x-www-form-urlencoded" (as produced by many browsers). 他们的API示例使用简单的“ Content-Type:应用程序/ x-www-form-urlencoded”(由许多浏览器生成)显示请求。

Try to set content-type explicitly, there is a setter method 尝试显式设置content-type,有一个setter方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM