Java – http / 1.1 400 error request on httpget with basic authentication

I made an application for httpget

My application uses its own certificate, so the server requires user login For certificates, I used this tutorial

For httpget, I have implemented this implementation:

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

            }

Why do I make client The execute (httpget) response is an HTTP / 1.1 400 error request Why? Is it correct to authenticate in my code?

Solution

The problem is the authorization header

We must use:

httpGet.addHeader("Authorization",Base64.NO_WRAP));

Substitute:

httpGet.addHeader("Authorization",Base64.DEFAULT));

Because the default parameter adds a "CR" line terminator at the end of the string, it is incorrect if you use it for that title

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>