使用 apache httpcomponents 进行 Http 身份验证

发布于 2024-09-03 06:54:53 字数 1756 浏览 2 评论 0原文

我正在尝试使用 apache httpcomponents 4.0.1 开发一个 java http 客户端。该客户端调用页面“https://myHost/myPage”。此页面在服务器上受具有登录表单身份验证的 JNDIRealm 保护,因此当我尝试获取 https://myHost/myPage< /a> 我得到一个登录页面。我尝试使用以下代码绕过它,但没有成功:

//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。如何应用我的凭据参数来避免此登录表单?

任何帮助都会很棒

I am trying to develop a java http client with apache httpcomponents 4.0.1. This client calls the page "https://myHost/myPage". This page is protected on the server by a JNDIRealm with a login form authentication, so when I try to get https://myHost/myPage I get a login page. I tried to bypass it unsuccessfully with the following code :

//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);

When I run this code, I always get the login page, not myPage. How can I apply my credential parameters to avoid this login form?

Any help would be fantastic

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月依秋水 2024-09-10 06:54:53

HttpClient不支持表单登录。您想要做的是基本身份验证,它不适用于表单登录。

您可以简单地跟踪登录页面的表单发布并从 HttpClient 发送 POST 请求。

HttpClient doesn't support form login. What you are trying to do is Basic Auth, which does't work with form login.

You can simply trace the form post for login page and send the POST request from HttpClient.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文