c# - Optimize the performance of using HttpClient and TPL to validate proxies -
i'm trying use httpclient , tpl validate proxy addresses. i'm using simple , i've set servicepointmanager.defaultconnectionlimit = 100;
before start.
the problem i've found result varies lot between consecutive runs. there can 4 valid proxies , run 1 second after give 194 valid proxies. i'm concerned maybe should handle task throttling. should myself here?
or there other issues should try handle?
internal class validator { private readonly concurrentdictionary<string, long> _validatedproxydic = new concurrentdictionary<string, long>(); private async task<tuple<bool, long>> validateproxy(tuple<string, string, string> tuple) { try { string proxy = tuple.item1; string url = tuple.item2; string pattern = tuple.item3; var handler = new httpclienthandler { cookiecontainer = new cookiecontainer(), automaticdecompression = decompressionmethods.gzip | decompressionmethods.deflate, proxy = new webproxy(proxy, true), useproxy = true }; var client = new httpclient(handler); client.defaultrequestheaders.expectcontinue = false; client.defaultrequestheaders.add("user-agent", "mozilla/5.0 (compatible; msie 10.0; windows nt 6.2; trident/6.0)"); client.defaultrequestheaders.add("connection", "keep-alive"); client.defaultrequestheaders.add("accept", "*/*"); client.timeout = timespan.frommilliseconds(10000); var stopwatch = stopwatch.startnew(); var response = await client.getasync(url); var str = await response.content.readasstringasync(); if (str.contains(pattern)) _validatedproxydic.tryadd(proxy, stopwatch.elapsedmilliseconds); return new tuple<bool, long>(str.contains(pattern), stopwatch.elapsedmilliseconds); } catch (exception ex) { } return new tuple<bool, long>(false, -1); } public void foo(ienumerable<string> proxylist, string loginurl, string keyword) { var tasklist = new list<task<tuple<bool, long>>>(); foreach (var proxy in proxylist) { var tuple = new tuple<string, string, string>(proxy, loginurl, keyword); var task = task.run(() => validateproxy(tuple)); tasklist.add(task); } var result = task.whenall(tasklist).result; var validatedproxysortedlist = pair in _validatedproxydic orderby pair.value ascending select pair; using (var sw = new streamwriter("proxyvalidatedlistsorted.txt")) { foreach (var proxy in validatedproxysortedlist) sw.writeline(proxy.key + "\t" + proxy.value); } console.writeline("found {0} proxies.", validatedproxysortedlist.count()); } }
Comments
Post a Comment