如何使用httpclient登录网站?

时间:2011-07-20 17:17:40

标签: java httpclient

我可以使用以下代码登录应用程序。现在,每当我尝试访问应用程序中的任何链接时,它都会再次将我重定向到登录页面。

登录后和执行get方法后,我得到了以下响应标题。

响应标题:

   status code: HTTP/1.1 302 Object moved
Cache-Control = private
Content-Length = 270
Content-Type = text/html
catid=%7B9B181EE2%2D1F13%2D4E31%2D9279%2D786364B46B82%7D&regmode=existentmember&loginmode=loginsuccess
Set-Cookie = cartId=; path=/
Set-Cookie = loginId=%7B49C7B309%2DA3F7%2D4E2D%2DB5D6%2D39FE0AA3319E%7D%7BD73CF8E4%2DFB79%2D4D92%2DB8F9%2DE0B3DB6ABE6A%7D; expires=Thu, 28-Jul-2011 06:05:28 GMT; path=/
Set-Cookie = ASPSESSIONIDCQTABSCB=FPGEGHADKGCOODKIPILEAFPO; path=/
Date = Tue, 26 Jul 2011 06:05:28 GMT
 CompareId=%7BF48141FB%2D43F1%2D4561%2D9FB5%2D1E297799B6AE%7D%7B246DE50A%2D618B%2D4E5E%2DBC3A%2D0D9D5C318FAA%7D; expires=Fri, 20-Jul-2012 16:53:22 GMT; path=/
    Set-Cookie = ASPSESSIONIDASTAASDB=KDPFOLADKCNPCJGNLDPMBKNF; path=/
    Date = Wed, 20 Jul 2011 16:53:22 GMT

以下是代码

HttpClient httpclient = new HttpClient();
            httpclient.getHostConfiguration().setHost(host);
            httpclient.getParams().setParameter("http.connection.timeout",
                    new Integer(20000));
            httpclient.getParams().setParameter("http.socket.timeout",
                    new Integer(20000));

            PostMethod postMethod = new PostMethod(actionUrl);
             // Prepare login parameters
            NameValuePair[] data = {
                new NameValuePair("MemberLoginName","username"),
                new NameValuePair("MemberLoginPwd","password")
            };

            postMethod.setRequestBody(data);


            // Provide custom retry handler is necessary
            method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                    new DefaultHttpMethodRetryHandler(3, false));*/
            BufferedReader reader = null;

                // Execute the method.
                int statusCode = httpclient.executeMethod(postMethod);

                if (statusCode != HttpStatus.SC_OK) {
                    System.err.println("Method failed: "
                            + postMethod.getStatusLine());
                    strHtmlContent = null;
                } else {
                    System.out.println("status code: "
                            + postMethod.getStatusLine());

           GetMethod method = new GetMethod(nextUrl);

请建议我在上面的代码中进行更改。

由于

2 个答案:

答案 0 :(得分:1)

让您的程序识别https网址并使用如下所示的内容。如果您只使用http。

,则可以删除密钥库和密钥
 /**
 * Initialize the HTTP/S connection (if needed)
 *
 * @param  keystoreFile  the full path of the keystore file
 * @param  keystorePass  the password for the keystore file
 */


    private void initHttps(String keystoreFile, String keystorePass)
   {
        // check if the URL uses HTTP/S
        if (url.toLowerCase().startsWith(HTTPS_PROTOCOL))
        {
            print("Initializing HTTP/S protocol...");
            // set the system properties needed for HTTP/S
            System.setProperty("javax.net.ssl.keyStore", keystoreFile);
            System.setProperty("javax.net.ssl.keyStorePassword", keystorePass);
            System.setProperty("javax.net.ssl.keyStoreType", "JKS");
            System.setProperty("javax.net.ssl.trustStore", keystoreFile);
            System.setProperty("javax.net.ssl.trustStorePassword", keystorePass);
            System.setProperty("javax.protocol.handler.pkgs",
                "com.sun.net.ssl.internal.www.protocol");
            //int addProvider = Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier()
            {   // fix a HTTP/S handshake issue
                public boolean verify(String hostName, SSLSession session)
                {   // Verify that the host name is acceptable 
                    return true;
                }
            });
       }
   }

答案 1 :(得分:0)

您可能需要设置一些额外的标志:

httpclient.getParams().setParameter("http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY);
httpclient.getParams().setParameter("http.protocol.single-cookie-header", Boolean.TRUE);