2010-01-09 46 views
2

我需要幫助,瞭解在向服務器發佈帖子後如何獲得重定向。 首先,我需要做一個獲取從服務器獲取一些cookie。然後我用cookies和其他參數進行發佈。服務器然後用302重定向來回答。我如何獲得重定向的網址?使用Java獲取重定向URL org.apache.http.client

代碼如下所示:

HttpGet get = new HttpGet(urlOne); 

try { 
    //Creating a local instance of cookie store. 
    CookieStore cookieJar = new BasicCookieStore(); 

    // Creating a local HTTP context 
    HttpContext localContext = new BasicHttpContext(); 

    // Bind custom cookie store to the local context 
    localContext.setAttribute(ClientContext.COOKIE_STORE, cookieJar); 

    HttpResponse response = httpClient.execute(get, localContext); 
    HttpEntity entity = response.getEntity(); 

    System.out.println("------------------GET----------------------"); 
    System.out.println(response.getStatusLine()); 
    if (entity != null) { 
     System.out.println("Response content length: " + entity.getContentLength()); 
    } 

    // Print out cookies obtained from server 
    List<Cookie> cookies = cookieJar.getCookies(); 
    for (int i = 0; i < cookies.size(); i++) { 
     System.out.println("Local cookie: " + cookies.get(i)); 
    }   

    if (entity != null) { 
     entity.consumeContent(); 
    } 
    System.out.println("------------------GET-END---------------------"); 

    // Create a new post 
    HttpPost post = new HttpPost(urlTwo); 
    post.setHeader("Content-Type", "application/x-www-form-urlencoded"); 

    // Add params 
    HttpParams params = new BasicHttpParams(); 
    params.setParameter("action", "search"); 
    params.setParameter("word", "hello"); 

    post.setParams(params); 

    //Execute 
    HttpResponse response2 = httpClient.execute(post, localContext); 
+0

這是HttpClient 3或4嗎? – skaffman 2010-01-09 14:07:10

+0

Android中使用的一個。我不確定哪個版本。 – jakob 2010-01-09 14:53:07

回答

1

我假設您想要自動執行瀏覽器操作並維護會話,以便您可以訪問那些需要維護會話的頁面。

我不知道如何通過org.apache.http.client API。如果您不限制使用org.apache.http.client API並可以使用其他API,那麼您可以使用HtmlUnit API,否則您可以忽略其餘的答案。

維護會話和自動化通過HtmlUnit瀏覽器操作可如下進行:

import com.gargoylesoftware.htmlunit.*; 
import com.gargoylesoftware.htmlunit.html.*; 

final WebClient webClient = new WebClient(); 
    try { 
     webClient.setJavaScriptEnabled(true); 
     webClient.setThrowExceptionOnScriptError(false); 
     webClient.setCssEnabled(true); 
     webClient.setUseInsecureSSL(true); 
     webClient.setRedirectEnabled(true); 

     HtmlPage loginPage = webClient.getPage(new URL("https://www.orkut.com/")); 
     System.out.println(loginPage.getTitleText()); 
     List<HtmlForm> forms = loginPage.getForms(); 
     HtmlForm loginForm = forms.get(0); 
     HtmlTextInput username = loginForm.getInputByName("Email"); 
     HtmlPasswordInput password = loginForm.getInputByName("Passwd"); 
     HtmlInput submit = loginForm.getInputByName("signIn"); 
     username.setValueAttribute("username"); 
     password.setValueAttribute("password"); 
     HtmlPage homePage = submit.click();. 
     Thread.sleep(10 * 1000); 
     HtmlPage homePageFrame = (HtmlPage) homePage.getFrameByName("orkutFrame").getEnclosedPage(); 
     HtmlPage communitiesTestPage = (HtmlPage) webClient.openWindow(new URL("http://www.orkut.co.in/Main#Community?cmm=1"), "CommunitiesWindow").getEnclosedPage(); 
    }catch(java.security.GeneralSecurityException e) { 
     e.printStackTrace(); 
    }catch(java.io.IOException e) { 
     e.printStackTrace(); 
    }catch(InterruptedException e) { 
     e.printStackTrace(); 
    } 

    WebWindow ww = webClient.getWebWindowByName("CommunitiesWindow"); 
    WebRequestSettings wrs1 = new WebRequestSettings(URL); // URL is the url that requires authentication first 

正如你可以看到,如何在上面的代碼自動化瀏覽器操作,以及它如何自動維護會話。我們並不需要手動處理Cookie或URLReDirect ...

+0

嗨,謝謝,但我需要使用該庫。 – jakob 2010-01-09 15:25:19

1

還有就是我在Java出現約一個簡單的方法。以下是步驟:

  1. 創建HttpUrl連接。
  2. 設置HttpURLConnection.setFollowRedirects(true); //這應該是默認爲true
  3. 通過您的HttpUrlConnection對象調用連接;
  4. 連接通過您的HttpUrlConnection對象調用getHeadersFields()後;
  5. 通過在上面的HttpUrlConnection對象上調用getUrl()獲取重定向的URL;

還有另一種使用HTTP標題中的位置字段獲取它的方法,但有時我們沒有在標題中獲取位置字段。至少它對我沒有任何作用。但上面的方法,它肯定有效。