谢谢你的时间!
设置: 我编写了一个JAVA REST客户端,它使用用户名/密码进行身份验证并返回JSON。
问题:
这是我得到的例外:
线程中的异常" main" java.io.IOException:服务器返回HTTP响应代码:401为URL: 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
答案 0 :(得分:0)
管理解决此问题。这些是问题:
此行需要更正,
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
*/