簡體   English   中英

具有基本身份驗證的httpGet上的HTTP / 1.1 400錯誤請求

[英]HTTP/1.1 400 Bad Request on httpGet with Base Authentication

我做了一個做httpGet的應用程序。

我的應用程序使用自己的證書,因此服務器需要用戶登錄。 對於證書,我使用了教程。

對於httpGet,我已經實現了這個實現:

 HttpClient client = new CustClient(getApplicationContext()); // HttpClient that uses my certificates



               // Example send http request
               final String url = "https://ip:port/";// <--in  my implementation i've a right url
               HttpResponse response = null;

               HttpGet httpGet = new HttpGet(url);

               //login
               httpGet.addHeader("Authorization", "Basic "+Base64.encodeToString("root:root".getBytes(),Base64.DEFAULT));

               StringBuilder testo = null;
               try {
                response = client.execute(httpGet);
                InputStream contenuto = response.getEntity().getContent();
                BufferedReader rd = new BufferedReader(new InputStreamReader(contenuto));

                String line;
                // Read response until the end
                while ((line = rd.readLine()) != null) { 

                    testo.append(line); 
                }
            } catch (Exception e) {

            } 

為什么我使client.execute(httpGet)響應是HTTP / 1.1 400錯誤請求 為什么? 在我的代碼中進行身份驗證是否正確?

問題是Authorization標頭。

我們必須使用:

httpGet.addHeader("Authorization", "Basic "+Base64.encodeToString("root:root".getBytes(),Base64.NO_WRAP));

代替:

httpGet.addHeader("Authorization", "Basic "+Base64.encodeToString("root:root".getBytes(),Base64.DEFAULT));

因為DEFAULT參數在字符串的末尾添加“CR”行終止符,如果你將它用於那個標題則它是不正確的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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