我有一个公共函数来获取网页 URL 的页面源。这是功能。
Public Function gethtml(ByVal id As Integer)
Try
ListView1.Items(id).SubItems(6).Text = "Downloading HTML..."
Dim Response3 As HttpWebResponse
Dim pagesource3 As String
Dim datastream As Stream
Dim postdata As String = ""
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postdata)
Dim Request3 As HttpWebRequest = HttpWebRequest.Create("http://mysite.com/ip.php")
Request3.Referer = "http://google.com/"
Request3.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"
Request3.KeepAlive = True
Request3.Timeout = "5000"
Request3.AllowAutoRedirect = True
If useproxycheckbox.Checked = True Then
Dim words As String() = ListView1.Items(id).SubItems(9).Text.Split(New Char() {":"c})
Request3.Proxy = New WebProxy(words(0).ToString & ":" & words(1).ToString)
Request3.UseDefaultCredentials = False
Request3.Proxy.Credentials = New System.Net.NetworkCredential(words(2).ToString, words(3).ToString)
End If
Request3.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.43 Safari/537.31"
Request3.CookieContainer = CC
Request3.Method = WebRequestMethods.Http.Get
Request3.ContentType = "application/x-www-form-urlencoded"
Response3 = Request3.GetResponse()
Dim Stream3 As New StreamReader(Response3.GetResponseStream())
pagesource3 = Stream3.ReadToEnd()
Response3.Close()
Stream3.Close()
ListView1.Items(id).SubItems(4).Text = pagesource3
Catch e1 As ProtocolViolationException
recheck(id)
ListView1.Items(id).SubItems(6).Text = "Error1"
Catch e2 As WebException
recheck(id)
ListView1.Items(id).SubItems(6).Text = "Error2"
End Try
End Function
问题是它在第一次调用时效果很好
gethtml(0)
但在那之后,它不使用我提供给它的代理。它只是使用第一个请求中使用的相同代理。我正在使用线程调用此函数,因为如果我在主线程上使用它,我将冻结 UI。我做错了什么?我是 VB.net 的菜鸟
谢谢。
更新:刚刚发现这个问题只发生在具有 USERNAME:PASSWORD 基本身份验证的代理上。帮我!