Take the 2-minute tour ×
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 '13 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 '13 at 19:23
 
you didn't post the whole code block, how do you decide what aReferer and aMethod –  Malachi Nov 14 '13 at 20:14
2  
(Just realized that's like 6 months old) –  Mat's Mug Nov 14 '13 at 23:27
1  
this code will continuously run if it encounters an error in the loop, and there is no way of coming out of it, or knowing what the exception is. –  Malachi Nov 14 '13 at 23:56
show 1 more comment

closed as unclear what you're asking by Malachi, 200_success, rolfl, Mat's Mug, Nikita Brizhak Nov 15 '13 at 11:52

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

Browse other questions tagged or ask your own question.