簡體   English   中英

Apache 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM