Answer the question
In order to leave comments, you need to log in
Lots of parallel httpwebrequest through different proxies. How to get maximum speed?
I am writing an application for parsing a bookmaker's website. The final goal is to get fresh html of the site as often and quickly as possible (it contains the information I need). Since the bookmaker bans ip if too many requests to the site are made from it, I use a collection of proxies (from 50 to 100 pieces) to distribute requests between.
Program language - C#. I am using asynchronous requests (httpwebrequest) like this:
ServicePointManager.DefaultConnectionLimit = 1000;
ServicePointManager.Expect100Continue = false;
ServicePointManager.UseNagleAlgorithm = false;
for (var i = 0; i < proxyCollection.Count; i++)
{
var proxyLocal = proxyCollection[i];
var iLocal = i;
Task.Run(async () =>
{
var httpWebRequest = (HttpWebRequest) WebRequest.Create(String.Format("https://bookmaker.com/page{0}", iLocal));
httpWebRequest.Proxy = proxyLocal;
httpWebRequest.PreAuthenticate = true;
httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (var httpWebResponse = (HttpWebResponse) await httpWebRequest.GetResponseAsync())
using (var responseStream = httpWebResponse.GetResponseStream())
using (var streamReader = new StreamReader(responseStream))
{
var stringContent = await streamReader.ReadToEndAsync();
//Тут я обрабатываю данные, которые получил с сайта. Обработка идет очень быстро - проблема не в ней
ProcessStringContent(stringContent);
}
});
}
Answer the question
In order to leave comments, you need to log in
Those. you've boxed yourself into Task.Run and async/await, and then you ask why. Task.Run does not guarantee that everything will run in separate threads, in fact there will be a queue and a ThreadPool. And using await ends up lining everything up into a very small number of threads. Here it is necessary to do it the old fashioned way, with old mechanisms, it will be more effective. BeginGetResponse/BeginGetRequestStream will help you and you won't need many streams.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question