Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
bool sucess = false;

var htmlDocument = new HtmlDocument();
while (!sucess)
{
    try
    {
        var httpWebRequest = (HttpWebRequest) WebRequest.Create(aUrl);
        httpWebRequest.CookieContainer = Cookies;
        httpWebRequest.UserAgent = "Mozilla/5.0 (Windows NT 6.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1";

        httpWebRequest.AllowAutoRedirect = true;

        httpWebRequest.Pipelined = true;

                httpWebRequest.KeepAlive = true;

                httpWebRequest.ServicePoint.ConnectionLimit = 3000000;

        httpWebRequest.Referer = aReferer;
        httpWebRequest.Method = aMethod;
        httpWebRequest.AutomaticDecompression = DecompressionMethods.GZip;
        httpWebRequest.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        httpWebRequest.Headers.Add("X-Requested-With", "XMLHttpRequest");
        httpWebRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";

        if (aMethod == "POST")
        {
            using (var stream = httpWebRequest.GetRequestStream())
            using (var sw = new StreamWriter(stream))
            {
                sw.Write(aParameter);
                sw.Flush();
            }
        }
        using (var response = (HttpWebResponse) httpWebRequest.GetResponse())
        using (var stream = response.GetResponseStream())
        {
            htmlDocument.LoadHtml(new StreamReader(stream, Encoding.UTF8).ReadToEnd());
            //htmlDocument.Load(stream, Encoding.UTF8);
        }
        sucess = true;
    }
    catch (Exception)
    {
        sucess = false;
    }
}
share|improve this question
Could you explain what exactly are you looking for? Is the code too slow? – svick May 8 at 16:27
Yes, i have added two more attributes in it, pipelined = true and keepalive = true. These both are properties of httpwebrequest object. I have also set the httpwebrequest.ServicePoint.ConnectionLimit = 300000. These are few tips i got in order to speed it up, it does speed up a lot but i want it to optimize even more – user1386926 May 8 at 19:23

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.