I have a function which checks if proxies are working or not, I decided to use Async Tasks to achieve this goal because it took way too much time to do it one by one, so I implemented this code:
var asyncTasks = new Task[toCheck.Count];
for (int i = 0; i < toCheck.Count; i++)
{
Console.SetCursorPosition(0, Console.CursorTop);
Console.Write("Checking... {0} proxies left {1} proxies working", toCheckCount, working.Count);
asyncTasks[i] = check(toCheck[i]);
toCheckCount -= 1;
}
Task.WaitAll(asyncTasks);
public static async Task check(string proxy)
{
ServicePointManager.Expect100Continue = true;
ServicePointManager.SecurityProtocol = SecurityProtocolType.SystemDefault;
var proxyObj = new WebProxy(proxy);
proxyObj.Address = new Uri($"socks4://{proxy}");
var handler = new HttpClientHandler
{
Proxy = proxyObj
};
var httpClient = new HttpClient(handler);
try
{
httpResponseMessage response = await httpClient.GetAsync("https://www.google.com/");
working.Add(proxy);
}
catch (HttpRequestException) { }
httpClient.Dispose();
}
This code is working in Visual Studio without problems, it works both in debug and release mode, however when I try to execute the .exe in the net6.0 folder the program runs through the whole code in a second and throws various exceptions ranging from request aborted to SSL connection couldn't be established to other common HTTP-related issues which stem from bad proxies, nothing unusual since this is exactly how the code checks if proxies work or not.
The problem is that the errors just don't stop and in the end 0 working proxies are returned. These results are obviously wrong since the same exact code yields the working proxies correctly in Visual Studio, so at this point I really have no clue what is going on here. I already searched for people that had similar issues but the results only said to await the code which I obviously already do, besides that switching the line
httpResponseMessage response = await httpClient.GetAsync("https://www.google.com/");
to:
httpResponseMessage response = httpClient.GetAsync("https://www.google.com/").Result;
worked, however, this is not a viable fix since it renders the whole purpose of this task useless.
//Edit To clarify I consider the program to be working, if it puts the proxies which sucessfully connected to google without exceptions in the List of working proxies. If an exception is encountered during the request the used proxy is ignored. this is exactly what happens when ran through VS, however if I run it via the .exe file directly it just instantly throws HttpExceptions on every single proxy, leading to the program showing 0 working proxies. This is wrong since you can test a proxy list in VS and see that x proxies are working and then check the same list through the .exe file and see that seemingly none are working, this result is false since it even marks good proxies as not working.