views:

193

answers:

2

I have always used this code to donwload the html source from a web page:

private string GetHtml(string url)
{
  HttpWebRequest web = (HttpWebRequest)WebRequest.Create(url);
  web.Method = "GET";
  WebResponse resp = web.GetResponse();
  Stream istrm = resp.GetResponseStream();
  StreamReader sr = new StreamReader(istrm);
  string html = sr.ReadToEnd();
  sr.Close();
  resp.Close();
  return html;
}

But it returns an empty string when url has this value:

http://actas.rfef.es/actas/RFEF%5FCmpJornada?cod%5Fprimaria=1000144&CodCategoria=100

+2  A: 

Use a cookiejar (and for cleaning up, use some usings)

private string GetHtml(string url)
{
    HttpWebRequest web = (HttpWebRequest)WebRequest.Create(url);
    web.Method = "GET";
    CookieContainer cookieJar = new CookieContainer();
    web.CookieContainer = cookieJar;
    using (WebResponse resp = web.GetResponse())
    {
        using (Stream istrm = resp.GetResponseStream())
        {
            using (StreamReader sr = new StreamReader(istrm))
            {
                string html = sr.ReadToEnd();
                sr.Close();
                resp.Close();
                return html;
            }
        }
    }
}

Also, what can also use if things start going bad again:

    web.Accept = "text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
    web.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 (.NET CLR 3.5.30729)";
    web.Referer = url;
Jeroen
Nice answer! :-)
REA_ANDREW
It works! Thankyou!
Victor
A: 

Thanks a lot!!!

Flash