加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 具有基本身份验证的httpGet上的HTTP / 1.1 400错误请求

发布时间:2020-12-15 05:10:02 所属栏目:Java 来源:网络整理
导读:我做了一个做httpGet的应用程序. 我的应用程序使用自己的证书,因此服务器需要用户登录. 对于证书,我使用了this教程. 对于httpGet,我已经实现了这个实现: HttpClient client = new CustClient(getApplicationContext()); // HttpClient that uses my certifi
我做了一个做httpGet的应用程序.

我的应用程序使用自己的证书,因此服务器需要用户登录.
对于证书,我使用了this教程.

对于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",Base64.NO_WRAP));

代替:

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

因为DEFAULT参数在字符串的末尾添加“CR”行终止符,如果你将它用于那个标题则它是不正确的.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读