HTTPCLIENT:asp页面上的https协议认证

时间:2011-02-22 12:04:29

标签: httpclient

我需要访问运行'https'协议的asp页面。 我在认证部分本身遇到问题。响应对象返回“HTTP / 1.1 200 OK”,但我被重定向到Login页面本身。

以下是我的代码:

public FileDownloadHttpWrapper(String url,String username, String password)
{
    SchemeRegistry supportedSchemes = new SchemeRegistry();
    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);
    mClient = new DefaultHttpClient(ccm,params);            
    mClient.getCredentialsProvider().setCredentials(
        new AuthScope(null,AuthScope.ANY_PORT),
        new UsernamePasswordCredentials(username, password)
        );
}

private Object getRequest(String url)
{
    HttpGet get = new HttpGet("/EvalMuniMKT/mainmenu.asp");
    HttpHost target = new HttpHost(url, 443, "https");

    try
    {
        // execute the GET
        HttpResponse resp = mClient.execute(target,get);

        HttpEntity entity = resp.getEntity();

        System.out.println(resp.getStatusLine());            
        System.out.println(EntityUtils.toString(entity));   
    } 
    catch(Exception ex)
    {
        ex.printStackTrace();
    }
    finally
    {
        // release any connection resources used by the method    
    }

    return null;
}

1 个答案:

答案 0 :(得分:0)

我认为asp网站使用基于表单的登录(会话),而不是 http基本身份验证。为此,您可能需要登录表单并在上下文中保存cookie并在执行实际请求时传递上下文。

类似的东西:

        CookieStore cookieStore = new BasicCookieStore();
    // Create local HTTP context
    HttpContext localContext = new BasicHttpContext();
    // Bind custom cookie store to the local context
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
          hc.execute(httpget, localContext);