從網頁上讀取源代碼,並寫入文件

- 中國WEB開發者網絡 (http://www.webasp.net)
-- 技術教程 (http://www.webasp.net/article/)
--- 從網頁上讀取源代碼,並寫入文件 (http://www.webasp.net/article/16/15236.htm)
-- 作者:未知
-- 發佈日期: 2004-12-02
Private Sub DownloadData(ByVal URLString As String, ByVal LocalFile As String) 'LocalFile 是文件的一個完全路徑 (包括*.exe)
Try

'HttpWebRequest 類對 WebRequest 中定義的屬性和方法提供支持',也對使用戶能夠直接與使用 HTTP 的服務器交互的附加屬性和方法提供支持。
Dim httpReq As System.Net.HttpWebRequest

' HttpWebResponse 類用於生成發送 HTTP 請求和接收 HTTP 響'應的 HTTP 獨立客戶端應用程序。
Dim httpResp As System.Net.HttpWebResponse

Dim httpURL As New System.Uri(URLString)

httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
httpReq.Method = "GET"
httpResp = CType(httpReq.GetResponse(), HttpWebResponse)

Dim reader As StreamReader = New StreamReader(httpResp.GetResponseStream) '如是中文,要設置編碼格式為「GB2312」。
Dim respHTML As String = reader.ReadToEnd() 'respHTML就是網頁源代碼
Dim Sw As StreamWriter = File.CreateText(LocalFile)

Sw.Write(respHTML)
Sw.Close()

httpResp.Close()

Catch e As Exception
Console.WriteLine("GetSource出現問題:{0},{1}", e.Message, URLString)
End Try
End Sub



webasp.net