2011-12-28 86 views
1

我想提取一個網站的來源,並且我研究了一點點和許多解決方案指向使用HTTPClient和HTTPContext,但問題是我無法使用URL來獲取此來源。我使用的網站是基於登錄名的,無論您登錄的是誰,它都顯示相同的URL(但是,當然,根據用戶要提取的信息是不同的)。因此,我想知道是否有辦法直接從webview或類似的東西獲取源代碼。總之,我不能使用中間URL,因爲它是統一的,基本上重定向到一個通用的登錄頁面。Android:提取html源代碼

對不起,如果我失去了一些東西;我是新來的。感謝您提前的幫助。

編輯

我已經找到了差異化的URL,它是每個用戶不同,但有一個(另)一個相關的問題: 使用jsoup,我可以做Jsoup.connect(」 。http://www.stackoverflow.com/「)獲得()HTML(); (用我想要訪問的URL替換URL),並且這實際上獲得了HTML源代碼,但是當我嘗試訪問受用戶/密碼保護的網站時,問題再次出現,它要求登錄信息。我需要能夠輸入一次用戶名和密碼,基本上將它存儲在某種臨時的東西(cookies/cache?)中,並保留這些信息讓jsoup每次要求基於某個源的某個源時停止查詢登錄憑證URL。我仍然無法找到一個方法來解決這個問題...

回答

1

那麼,如果我理解正確的(讓我知道如果我沒有):

如果用戶/密碼保護的,你應該發出一個HTTP POST(即是你從瀏覽器做的例子),並從該帖子獲得回覆?事情是這樣的:

http://www.informit.com/guides/content.aspx?g=java&seqNum=44

編輯:這裏是一個示例

我有一個頁面,看起來像這樣(它過於簡單,但儘管如此,這裏是):

<form action="../../j_spring_security_check" method="post" > 
     <input id="j_username" name="j_username" type="text" /> 
      <input id="j_password" name="j_password" type="password"/> 
        <input type="image" class="submit" id="login" name="login" /> 
</form> 

如果它位於網頁的哪個位置,則必須提供用戶名/密碼才能在此登錄頁面「後面」獲取實際內容。你真正發出的是一個HTTP POST這裏(我敢打賭,你的情況是一樣的)。

我們得到了相同的功能以編程的方式...

您需要的Apache HTTP客戶端庫(你很可能沒有它,但是這是最簡單的方式)。這是它的maven依賴。你正在爲Android做這個,對吧?從我讀過的內容來看,apache http client是Android中的默認設置。

<dependency> 
<groupId>commons-httpclient</groupId> 
<artifactId>commons-httpclient</artifactId> 
<version>3.1</version> 

import org.apache.commons.httpclient.Header; 
import org.apache.commons.httpclient.HttpClient; 
import org.apache.commons.httpclient.methods.GetMethod; 
import org.apache.commons.httpclient.methods.PostMethod; 

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

     HttpClient httpClient = new HttpClient(); 
     PostMethod postMethod = new PostMethod("http://localhost:20000/moika/moika/j_spring_security_check"); 
     postMethod.addParameter("j_username", "ACTUAL_USER"); 
     postMethod.addParameter("j_password", "ACTUAL_PASSWORD"); 

     try { 
      int status = httpClient.executeMethod(postMethod); 
      System.out.println("STATUS-->" + status); 

      if(status == 302){ 
       Header header = postMethod.getResponseHeader("location"); 
       String location = header.getValue(); 
       System.out.println("HEADER_VALUE-->" + location); 
       GetMethod getMethod = new GetMethod(location); 
       httpClient.executeMethod(getMethod); 
       String content = getMethod.getResponseBodyAsString(); 
       System.out.println("CONTENT-->" + content); 
      } 

      String contentInCaseOfNoRedirect = postMethod.getResponseBodyAsString(); 

     } catch (Exception exception){ 
      exception.printStackTrace(); 
     } 
    } 
} 

這可能看起來很怪異了一點,但我執行重定向(302),似乎與在RCF的問題,因此小的變通。

如果您不在服務器端重新執行任何操作,那麼您可以忽略檢查302的部分。

看看有什麼適合你的。

乾杯, 尤金。

+0

你的方法似乎是正確的軌道上的內容,但讓我困惑。考慮到這種情況,是否可以提供一些關於如何獲取網站html源代碼的示例代碼? – Kgrover 2011-12-28 19:06:06

+0

編輯答案。 Eugene – Eugene 2011-12-29 09:19:22

+0

我會試驗一下,讓你知道。感謝您的迴應! – Kgrover 2011-12-30 03:23:13

0

看到http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html

或檢查示例代碼

如何讀取URL

try{ 
     URL oracle = new URL("http://www.w3schools.com/html/html_tables.asp"); 
     URLConnection yc = oracle.openConnection(); 
     InputStream is = yc.getInputStream(); 
     String inputLine; 
     BufferedReader in = new BufferedReader(
       new InputStreamReader(
       yc.getInputStream())); 
     while ((inputLine = in.readLine()) != null) 
      System.out.println(inputLine); 
     in.close(); 

     }catch(Exception ex){ 
      ex.printStackTrace(); 
     } 
+0

由於早晨的情緒,我可能會變得緩慢,但是您是如何在答案中解決這部分問題的:「它顯示相同的URL(但是,當然,要提取的信息因用戶而異)」。只是簡單閱讀一個URL的內容,恕我直言,你根本沒有回答這個問題 – Eugene 2011-12-28 08:08:59

+0

是的,尤金,我完全同意我知道如何閱讀一個普通的URL的內容,但這裏的情況是不同的。 – Kgrover 2011-12-28 19:06:19