2011-07-20 63 views
2

我可以使用下面的代碼登錄到應用程序。現在,當我嘗試從應用程序訪問任何鏈接時登錄後,它會重新將我重定向到登錄頁面。如何使用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); 

請建議我改變了上面的代碼需要。

感謝

回答

1

讓你的程序來識別HTTPS URL,並使用一些thign像以下。如果您只使用http,則可以刪除密鑰庫和keypass。

/** 
* 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; 
       } 
      }); 
     } 
    } 
0

您可能需要設置幾個附加標誌:

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