从网页上读取源代码,并写入文件 - 中国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 |