VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > VB.net教程 >
  • vb.net获取取网页内容

具体代码如下(互相学习):

代码中有解决“远程服务器返回错误: (403) 已禁止。”问题的方法。

 ConnectNetWorkByIPC 连接IP.cn网站获取页面信息
  •  
    '*Parameter : N
  •  
    '*return : N
  •  
    '*Author : Miles_Mei
  •  
    '*Mail:****
  •  
    '*Date:2019/10/10 11:48:32
  •  
    '************************************************************************************
  •  
    Public Sub ConnectNetWorkByIPC()
  •  
    Dim url As String = "https://ip.cn/" '网站
  •  
    Dim httpRequest As System.Net.HttpWebRequest
  •  
    Dim httpResponse As System.Net.HttpWebResponse
  •  
    Dim httpURL As New System.Uri(url)
  •  
    httpRequest = CType(WebRequest.Create(httpURL), HttpWebRequest)
  •  
    '下面这个一定要加上,否则在某些网站没有会发生"远程服务器返回错误: (403) 已禁止。"具体是用户代理,浏览器内核,版本标识等信息,不是很了解
  •  
    httpRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Trident/4.0; QQWubi 133; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; CIBA; InfoPath.2)"
  •  
    httpRequest.Method = "GET"
  •  
    httpResponse = CType(httpRequest.GetResponse(), HttpWebResponse)
  •  
    httpRequest.KeepAlive = False
  •  
    Dim reader As StreamReader = New StreamReader(httpResponse.GetResponseStream, System.Text.Encoding.UTF8) '读取响应流中的数据
  •  
    Dim ResponseHTML As String = reader.ReadToEnd() 'respHTML就是网页内容
  •  
    Application.DoEvents()
  •  
    Dim strIP() As String = ResponseHTML.Split(New Char() {ControlChars.Lf, ControlChars.Cr}) '以回车换行逐条分析网页
  •  
    Application.DoEvents()
  •  
    Dim strResult() As String
  •  
    Dim strR() As String
  •  
    '分析内容获取字段
  •  
    For i As Integer = 0 To strIP.Count - 1
  •  
    If String.Compare("$('#result').html", Mid(strIP(i).Trim, 1, 17)) = 0 Then
  •  
    strR = Split(strIP(i), ">")
  •  
    strResult = Split(strR(3), "<")
  •  
    Net_IP = strResult(0).Trim
  •  
    strResult = Split(strR(7), "<")
  •  
    Net_Site = strResult(0).Trim
  •  
    strResult = Split(strR(11), "<")
  •  
    Net_Address = strResult(0).Trim
  •  
    End If
  •  
    Next
  •  
    End Sub
 

 


相关教程