在网络上找了很多关于网页下载的程序但都不能完整地得到web页的内容,以下这个函数解决了这个问题。
Private Function GetSource(ByVal url As String) As String Try Dim httpReq As System.Net.HttpWebRequest Dim httpResp As System.Net.HttpWebResponse Dim httpURL As New System.Uri(url) httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest) httpReq.Method = "GET" httpResp = CType(httpReq.GetResponse(), HttpWebResponse) Dim reader As StreamReader = _ New StreamReader(httpResp.GetResponseStream, System.Text.Encoding.GetEncoding("GB2312")) Dim respHTML As String = reader.ReadToEnd() Return respHTML httpResp.Close() Catch e As Exception Console.WriteLine("GetSource出现问题:{0},{1}", e.Message, url) End Try End Function
|