2016-03-08 62 views
0

我試圖以網頁形式下載字符串。有人可以解釋爲什麼下面的代碼不起作用嗎?將html下載爲字符串

Dim URL As String = "http://stackoverflow.com/" 
    Dim client As WebClient = New WebClient() 
    Dim data As Stream = client.OpenRead(URL) 
    Dim reader As StreamReader = New StreamReader(data) 
    Dim str As String = "" 
    str = reader.ReadLine() 
    Do While str.Length > 0 
     Console.WriteLine(str) 
     str = reader.ReadLine() 
    Loop 

當我運行它時,它永遠不會進入循環。

回答

3

給這個一杆...

Dim URL As String = "http://stackoverflow.com/" 
    Dim html As String = New WebClient().DownloadString(URL) 

更好的解決方案(發行版中的網絡堆棧正在使用的資源。此外,還要確保的(希望)需要時CLR清除這些了。)

Using client As New WebClient() 
    html = client.DownloadString(URL) 
End Using 
+0

它的工作原理!非常感謝! – MATH000

+0

很好,請參閱更新。此外,如果這有助於請標記爲答案。 – Codexer

+0

是的,在2分鐘內 – MATH000