繁体   English   中英

REST客户端返回HTTP响应代码:401

[英]REST Client returns HTTP response code: 401

谢谢你的时间!

设置:我编写了一个JAVA REST客户端,该客户端进行身份验证(使用用户名/密码)并返回JSON。

问题:

这是我得到的例外:

线程“主”中的异常java.io.IOException:服务器返回的HTTP响应代码:401用于URL: https://1.1.1.1/api/count : https://1.1.1.1/api/count

码:

public class AnotherDemo {
    static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
        new javax.net.ssl.HostnameVerifier(){

            public boolean verify(String hostname,
                    javax.net.ssl.SSLSession sslSession) {
                if (hostname.equals("localhost")) {
                    return true;
                }
                return false;
            }
        });
    }
    public static void main(String[] args) throws Exception{

           TrustManager[] trustAllCerts = new TrustManager[] {
                   new X509TrustManager() {
                      public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return null;
                      }

                      public void checkClientTrusted(X509Certificate[] certs, String authType) {  }

                      public void checkServerTrusted(X509Certificate[] certs, String authType) {  }

                   }
                };

                SSLContext sc = SSLContext.getInstance("SSL");
                sc.init(null, trustAllCerts, new java.security.SecureRandom());
                HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

                // Create all-trusting host name verifier
                HostnameVerifier allHostsValid = new HostnameVerifier() {
                    public boolean verify(String hostname, SSLSession session) {
                      return true;
                    }
                };
                // Install the all-trusting host verifier
                HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

        String urlString = "https://1.1.1.1/api/count";
        String username = "admin";
        String password = "admin";

        String usercredentials = username+":admin"+password;
        String basicAuth = "Basic"+ new String (new Base64().encode(usercredentials.getBytes())); 

        // pass encoded user name and password as header
        URL url = new URL(urlString);
//      URLConnection conn = url.openConnection();
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");
        conn.setRequestProperty("Authorization", "Basic " + basicAuth);
        conn.setRequestProperty("Accept", "application/json");
        BufferedReader r = new BufferedReader(new InputStreamReader(
                conn.getInputStream()));
        String line = r.readLine();
        while (line != null) {
            System.out.println(line);
            line = r.readLine();
        }
    }
}

有人可以告诉我我在做什么错吗?

如果我使用POSTMAN,一切正常! 我得到了JSON!

谢谢,R

设法解决了这个问题。 这些是问题:

此行需要更正,并且

String usercredentials = username+":admin"+password;
 String basicAuth = "Basic"+ new String (new Base64().encode(usercredentials.getBytes())); 

String usercredentials = username+":"+password;
String basicAuth = "Basic"+ new String (new Base64().encode(usercredentials.getBytes())); 

另外,对于SSL处理程序或此异常的问题,

com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

请添加以下LOC:

   TrustManager[] trustAllCerts = new TrustManager[] {
           new X509TrustManager() {
              public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
              }

              public void checkClientTrusted(X509Certificate[] certs, String authType) {  }

              public void checkServerTrusted(X509Certificate[] certs, String authType) {  }

           }
        };

        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
              return true;
            }
        };
        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
        /*
         * end of the fix
         */

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM