2013-10-30 99 views
1

我有這樣的代碼,女巫編譯和運行沒有問題。不能以編程方式執行登錄到網站

package isitup; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.cookie.Cookie; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.protocol.HTTP; 
import org.apache.http.util.EntityUtils; 

import java.util.ArrayList; 
import java.util.List; 
import org.apache.http.client.CookieStore; 
import org.apache.http.client.params.ClientPNames; 
import org.apache.http.cookie.CookieOrigin; 
import org.apache.http.cookie.CookieSpec; 
import org.apache.http.cookie.CookieSpecFactory; 
import org.apache.http.cookie.MalformedCookieException; 
import org.apache.http.impl.client.BasicCookieStore; 
import org.apache.http.impl.cookie.BrowserCompatSpec; 
import org.apache.http.params.HttpParams; 

public class Test { 
    public static void main(String args[]) 
    { 
     try 
     { 

      DefaultHttpClient httpclient = new DefaultHttpClient(); 

      HttpGet httpget = new HttpGet("http://www.cashialize.com/wp-login.php"); 

      HttpResponse response = httpclient.execute(httpget); 
      HttpEntity entity = response.getEntity(); 

      System.out.println("Login form get: " + response.getStatusLine()); 
      if (entity != null) { 
       EntityUtils.consume(entity); 
      } 
      System.out.println("Initial set of cookies:"); 
      List<Cookie> cookies = httpclient.getCookieStore().getCookies(); 
      if (cookies.isEmpty()) { 
       System.out.println("None"); 
      } else { 
       for (Cookie cooky : cookies) { 
        System.out.println("- " + cooky.toString()); 
       } 
      } 

      HttpPost httpost = new HttpPost("http://www.cashialize.com/wp-login.php"); 

      List <NameValuePair> nvps = new ArrayList<NameValuePair>(); 
      nvps.add(new BasicNameValuePair("log", "test")); 
      nvps.add(new BasicNameValuePair("pwd", "test")); 
      nvps.add(new BasicNameValuePair("rememberme", "forever")); 
      nvps.add(new BasicNameValuePair("redirect_to", "http://www.articlepub.com/wp-admin/")); 
      nvps.add(new BasicNameValuePair("testcookie", "1")); 
      nvps.add(new BasicNameValuePair("wp-submit", "Log In")); 


      httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8)); 

      response = httpclient.execute(httpost); 
      entity = response.getEntity(); 

      System.out.println("Login form get: " + response.getStatusLine()); 
      if (entity != null) { 
       EntityUtils.consume(entity); 
      } 

      System.out.println("Post logon cookies:"); 
      cookies = httpclient.getCookieStore().getCookies(); 
      if (cookies.isEmpty()) { 
       System.out.println("None"); 
      } else { 
       for (Cookie cooky : cookies) { 
        CookieStore cookieStore = new BasicCookieStore(); 

      // Bind custom cookie store to the local context 
      httpclient.setCookieStore(cookieStore); 
      CookieSpecFactory csf = new CookieSpecFactory() { 
       public CookieSpec newInstance(HttpParams params) { 
        return new BrowserCompatSpec() { 
         @Override 
         public void validate(Cookie cookie, CookieOrigin origin) 
           throws MalformedCookieException { 
          // allow all cookies 
         } 
        }; 
       } 
      }; 
      httpclient.getCookieSpecs().register("easy", csf); 
      httpclient.getParams().setParameter(ClientPNames.COOKIE_POLICY, "easy"); 
       } 
      } 




      HttpGet httpGet = new HttpGet("http://www.articlepub.com/wp-admin/profile.php"); 
      response = httpclient.execute(httpGet); 
      entity = response.getEntity(); 
      // System.out.println("Page Contents: " + EntityUtils.toString(entity)); 


      System.out.println("Login form get: " + response.getStatusLine()); 
      if (entity != null) { 
       EntityUtils.consume(entity); 
      } 

      System.out.println("Post get cookies:"); 
      cookies = httpclient.getCookieStore().getCookies(); 
      if (cookies.isEmpty()) { 
       System.out.println("None"); 
      } else { 
       for (Cookie cooky : cookies) { 
        System.out.println("- " + cooky.toString()); 
       } 
      } 


      httpclient.getConnectionManager().shutdown(); 

     } catch (Exception e) { 
      System.out.println(e); 

     } 

    } 

} 

的問題是,我不能讓用戶登錄我敢確定我錯過了cookie設置中的某些內容,但我可以發現它,因爲它是我第一次登錄嘗試之一。

控制檯輸出也似乎很好 - 唯一的問題是,我不能發現cookie。這裏是:

Login form get: HTTP/1.1 200 OK 
Initial set of cookies: 
None 
Login form get: HTTP/1.1 302 Found 
Post logon cookies: 
None 
Login form get: HTTP/1.1 200 OK 
Post get cookies: 
None 

請幫助我這個噩夢!我很確定我錯過了一些非常小的東西,但我無法弄清楚。

回答