我正在尝试用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。如何应用我的凭证参数以避免此登录表单?
任何帮助都会很棒
答案 0 :(得分:1)
HttpClient不支持表单登录。你要做的是Basic Auth,它不适用于表单登录。
您只需跟踪登录页面的表单帖子并从HttpClient发送POST请求。