【问题标题】:Http authentication with apache httpcomponents使用 apache httpcomponents 进行 Http 身份验证
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-12-20
      • 1970-01-01
      • 1970-01-01
      • 2016-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-17
      • 1970-01-01
      相关资源
      最近更新 更多