Updated Code:- Using SSL, still am getting the same error..
我正试图打开这个uri
https://some-host/a/getmeta?id=10 (this url is passed to proxi.jsp page)
这是我的proxi.jsp页面,我收到此错误需要HTTP / 1.1 401授权,当我传递我的凭据时。它为什么会这样发生..那个网站使用siteminder。
<%@ page language="java" import="
org.apache.http.HttpEntity,
org.apache.http.HttpResponse,
org.apache.http.auth.AuthScope,
org.apache.http.auth.UsernamePasswordCredentials,
org.apache.http.client.methods.HttpPost,
org.apache.http.client.methods.HttpGet,
org.apache.http.impl.client.DefaultHttpClient,
org.apache.http.util.EntityUtils,
java.io.InputStream,
java.io.InputStreamReader,
java.io.BufferedReader,
java.security.KeyStore,
java.io.FileInputStream,
java.io.File,
org.apache.http.conn.ssl.SSLSocketFactory,
org.apache.http.conn.scheme.Scheme,
javax.net.ssl.HostnameVerifier,
org.apache.http.impl.conn.SingleClientConnManager,
javax.net.ssl.HttpsURLConnection,
org.apache.http.conn.scheme.SchemeRegistry,
javax.net.ssl.SSLContext,
java.security.cert.X509Certificate,
javax.net.ssl.X509TrustManager,
javax.net.ssl.TrustManager,
org.apache.http.conn.ClientConnectionManager,
java.security.cert.CertificateException,
org.apache.http.conn.scheme.Scheme"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%
String a_Url = request.getParameter( "url" ) ;
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm"),
new UsernamePasswordCredentials("test", "pass"));
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
//FileInputStream instream = new FileInputStream(new File("my.keystore"));
InputStream instream = Thread.currentThread().getContextClassLoader().getResourceAsStream("my.keystore");
try {
trustStore.load(instream, "nopassword".toCharArray());
} finally {
try { instream.close(); } catch (Exception ignore) {}
}
/*
SSLSocketFactory socketFactory = new SSLSocketFactory(trustStore);
Scheme sch = new Scheme("https", 443, socketFactory);
httpclient.getConnectionManager().getSchemeRegistry().register(sch);
*/
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
}
public X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[]{tm}, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx);
ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
ClientConnectionManager ccm = httpclient.getConnectionManager();
SchemeRegistry sr = ccm.getSchemeRegistry();
sr.register(new Scheme("https", ssf, 443));
HttpGet httpget = new HttpGet(a_Url);
System.out.println("executing request" + httpget.getRequestLine());
HttpResponse res = httpclient.execute(httpget);
HttpEntity entity = res.getEntity();
System.out.println("----------------------------------------");
System.out.println(res.getStatusLine());
if (entity != null) {
System.out.println("Response content length: " + entity.getContentLength());
InputStream input = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
String ln = "";
while((ln = reader.readLine()) != null) {
out.println("During Get - " + ln);
}
entity.consumeContent();
}
EntityUtils.consume(entity);
}
catch (Throwable t) {
StackTraceElement[] x = t.getStackTrace();
for(int k=0;k<x.length;k++) {
out.println(x[k].toString());
}
//out.println();
t.printStackTrace();
}
finally {
// When HttpClient instance is no longer needed,
// shut down the connection manager to ensure
// immediate deallocation of all system resources
httpclient.getConnectionManager().shutdown();
}
%>
答案 0 :(得分:0)
您正在访问安全网站,但我在您的HttpClient代码中看不到任何SSL处理。您是否可以查看this page并在填写适当的空白后在独立客户端试用?
答案 1 :(得分:0)
首先,您发布的代码似乎并未配置http客户端以使用HTTPS
您缺少以下代码(至少对于org.apache.http.client.HttpClient
):
SSLSocketFactory sf = new SSLSocketFactory(sslcontext);
Scheme https = new Scheme("https", sf, 443);
httpclient.getConnectionManager().getSchemeRegistry().register(https);
您必须查看DefaultHttpClient
的教程
无论如何要看看发生了什么,你可以使用像wireshark这样的嗅探工具
SSL握手是可见的,您将能够看到连接失败并理解原因。
答案 2 :(得分:0)
除非您确定"realm"
是AuthScope
构造函数中的正确值,否则我建议您删除它或确定实际值应该是什么。
答案 3 :(得分:0)
更改以下行:new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, "realm")
到以下行:
new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT, AuthScope.ANY_REALM)