我想在使用它们之前检查列表中的代理是否正常工作,是否可能?
对于HTTP / HTTPS来说很容易,因为你只需要使用webclient而不是袜子?
我认为这可以适用于所有人
public static bool SoketConnect(string addresse)
{
string[] proxy = addresse.Split(':');
if (proxy.Count() == 2)
{
string host = proxy[0];
int port = Convert.ToInt32(proxy[1]);
var is_success = false;
try
{
var connsock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
connsock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.SendTimeout, 200);
System.Threading.Thread.Sleep(500);
var hip = IPAddress.Parse(host);
var ipep = new IPEndPoint(hip, port);
connsock.Connect(ipep);
try
{
byte[] outStream = System.Text.Encoding.ASCII.GetBytes("ping");
byte[] inStream = new byte[100];
connsock.Send(outStream);
connsock.ReceiveTimeout = 500;
connsock.Receive(inStream);
string message = System.Text.Encoding.ASCII.GetString(inStream);
foreach (var bytes in inStream)
{
if (bytes != 0)
{
is_success = true;
break;
}
}
connsock.Disconnect(false);
}
catch(Exception ex)
{
is_success = false;
}
connsock.Close();
}
catch (Exception ex)
{
is_success = false;
}
return is_success;
}
else
return false;
}
但我总是得到例外
"底层连接已关闭:连接已关闭 出乎意料&#34。 System.Net.WebException
tldr;如何检查我的袜子代理是否正常工作(回到如何使用socks代理)
编辑:仍然没有帮助,需要一个答案 EDIT2:只有ping代理才是正确的事情
答案 0 :(得分:2)
据我所知,您必须使用它们来查看它们是否正常工作。我建议以下其中一项:
ping
- here班级:
using System.Net.NetworkInformation;
private static bool CanPing(string address)
{
Ping myping = new Ping();
try
{
PingReply reply = myping.Send(address, 2000);
if (reply == null)
return false;
return (reply.Status == IPStatus.Success);
}
catch (PingException e)
{
return false;
}
}
做一个"什么是我的IP?"通过代理:
public static void TestProxies()
{
var lowp = new List<WebProxy> { new WebProxy("1.2.3.4", 8080), new WebProxy("5.6.7.8", 80) };
Parallel.ForEach(lowp, wp => {
var success = false;
var errorMsg = "";
var sw = new Stopwatch();
try
{
sw.Start();
var response = new RestClient
{
BaseUrl = "https://webapi.theproxisright.com/",
Proxy = wp
}.Execute(new RestRequest { Resource = "api/ip", Method = Method.GET, Timeout = 10000, RequestFormat = DataFormat.Json });
if (response.ErrorException != null)
throw response.ErrorException;
success = (response.Content == wp.Address.Host);
}
catch (Exception ex)
{
errorMsg = ex.Message;
}
finally
{
sw.Stop();
}
});
}