【发布时间】:2010-05-31 17:53:03
【问题描述】:
我正在尝试使用 apache httpcomponents 4.0.1 开发一个 java http 客户端。此客户端调用页面“https://myHost/myPage”。此页面在服务器上由具有登录表单身份验证的 JNDIRealm 保护,因此当我尝试获取 https://myHost/myPage 时,我得到一个登录页面。我尝试使用以下代码绕过它失败:
//I set my proxy
HttpHost proxy = new HttpHost("myProxyHost", myProxyPort);
//I add supported schemes
SchemeRegistry supportedSchemes = new SchemeRegistry();
supportedSchemes.register(new Scheme("http", PlainSocketFactory
.getSocketFactory(), 80));
supportedSchemes.register(new Scheme("https", SSLSocketFactory
.getSocketFactory(), 443));
// prepare parameters
HttpParams params = new BasicHttpParams();
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, "UTF-8");
HttpProtocolParams.setUseExpectContinue(params, true);
ClientConnectionManager ccm = new ThreadSafeClientConnManager(params,
supportedSchemes);
DefaultHttpClient httpclient = new DefaultHttpClient(ccm, params);
httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,
proxy);
//I add my authentication information
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("myHost/myPage", 443),
new UsernamePasswordCredentials("username", "password"));
HttpHost host = new HttpHost("myHost", 443, "https");
HttpGet req = new HttpGet("/myPage");
//show the page
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String rsp = httpClient.execute(host, req, responseHandler);
System.out.println(rsp);
当我运行这段代码时,我总是得到登录页面,而不是 myPage。如何应用我的凭据参数来避免此登录表单?
任何帮助都会很棒
【问题讨论】:
标签: java authentication httpclient