vb.net - 如何制作一个恒定的代理检查器
问题描述
我已经完成了修改公共代码的多线程代理检查器。但我需要它来不断检查文件。这个想法是该文件将经常更新,并且代理检查器将始终在执行中,检查更新文件中的代理。然后它将代理(所有代理,而不仅仅是工作代理)导出到 .properties 文件中。问题是:我无法读取文件,检查代理并导出它,因为我不断收到错误“文件已被使用”。另外,即使它是多线程的,我也觉得它很慢......我真的需要帮助。
我正在使用的代码:
Dim proxies As New List(Of String)
Public Sub check(stateInfo As Object)
Dim proxyString As String = CType(stateInfo, String)
Dim myProxy As WebProxy
Try
myProxy = New WebProxy(proxyString)
Dim r As HttpWebRequest = WebRequest.Create("http://www.google.com/")
r.UserAgent = "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.2 Safari/537.36"
r.Timeout = 3000
r.Proxy = myProxy
Dim re As HttpWebResponse = r.GetResponse()
ListBox1.Items.Add(proxyString & "={""proxy"":true}")
Catch ex As Exception
ListBox1.Items.Add(proxyString & "={""proxy"":false}")
End Try
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
Timer1.Stop()
Using sr As New StreamReader("D:\Arquivos PC\Desktop\proxytests.txt")
While Not sr.EndOfStream
proxies.Add(sr.ReadLine())
End While
End Using
Timer2.Start()
End Sub
'The problem with the timers is, it doesn't wait the task to be finished before starting the next task (timer)...
Private Sub Timer2_Tick(sender As Object, e As EventArgs) Handles Timer2.Tick
Timer2.Stop()
Dim threads As Integer = 100
ThreadPool.SetMaxThreads(threads, threads)
ThreadPool.SetMinThreads((threads / 2), (threads / 2))
For Each proxy In proxies
ThreadPool.QueueUserWorkItem(AddressOf check, proxy)
Next
Timer3.Start()
End Sub
Private Sub Timer3_Tick(sender As Object, e As EventArgs) Handles Timer3.Tick
Timer3.Stop()
If (ListBox1.Items.Count > 0) Then
Using sw As New StreamWriter("D:\Arquivos PC\Desktop\file.properties")
For Each line As String In ListBox1.Items
sw.WriteLine(line)
Next
End Using
End If
Timer4.Start()
End Sub
Private Sub Timer4_Tick(sender As Object, e As EventArgs) Handles Timer4.Tick
Timer4.Stop()
ListBox1.Items.Clear()
proxies.Clear()
Timer1.Start()
End Sub
解决方案
推荐阅读
- sql-server - 在祖先表中插入数据并从祖先表中检索数据
- python - 使用 BERT 模型进行推理时没有 batch_size
- javascript - setState 回调函数没有被调用?
- python - 为什么我不能在 PyCharm 中导入两个模块?
- excel - VBA 如何只运行 3 个潜艇中的 1 个?
- django - 在 Django manage.py runserver 默认输出中添加用户名和客户端 IP 地址
- android - 使用反射为私有方法编写测试
- java - 合并我的项目后,如何解决 razorpay 中的 com.razorpay.CheckoutActivity Class not found 异常?在合并之前它工作正常
- javascript - 如何为 prettyPhoto 实现自定义标记触发器
- java - 如何使用 URL 调用 python 脚本