[英]Java - sending HTTP parameters with apache.http.client.methods.HttpPost
[英]Apache HttpPost isn't sending the query parameters?
我使用的是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);
寻求他们的支持。 我的猜测是,他们的服务器已损坏,并且无法理解您的UrlEncodedFormEntity正在生成的“内容类型:application / x-www-form-urlencoded; charset = UTF-8”的POST请求。
他们的API示例使用简单的“ Content-Type:应用程序/ x-www-form-urlencoded”(由许多浏览器生成)显示请求。
尝试显式设置content-type,有一个setter方法
post.getEntity().setContentType("application/x-www-form-urlencoded")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.