In case you were wondering, WebClient handles GZIP encoded content.
private static string GetPageMarkup(string uri)
{
string pageData = null;
using (WebClient client = new WebClient())
{
pageData = client.DownloadString(uri);
}
return pageData;
}
If you want to send a CookieContainer with the GET Request, you have to do smoething special:
class WebClientEx : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
(request as HttpWebRequest).CookieContainer = PersistentCookies.GetCookieContainerForUrl(address);
}
return request;
}
}
class PersistentCookies
{
// to get persistent cookies for TinyPic
[DllImport("wininet.dll", CharSet=CharSet.Auto , SetLastError=true)]
private static extern bool InternetGetCookie (string url, string name, StringBuilder data, ref int dataSize);
private static string RetrieveIECookiesForUrl(string url)
{
StringBuilder cookieHeader = new StringBuilder(new String(' ', 256), 256);
int datasize = cookieHeader.Length;
if (!InternetGetCookie(url, null, cookieHeader, ref datasize))
{
if (datasize < 0)
return String.Empty;
cookieHeader = new StringBuilder(datasize); // resize with new datasize
InternetGetCookie(url, null, cookieHeader, ref datasize);
}
// result is like this: "KEY=Value; KEY2=what ever"
return cookieHeader.ToString();
}
public static CookieContainer GetCookieContainerForUrl(string url)
{
return GetCookieContainerForUrl(new Uri(url));
}
public static CookieContainer GetCookieContainerForUrl(Uri url)
{
CookieContainer container = new CookieContainer();
string cookieHeaders = RetrieveIECookiesForUrl(url.AbsoluteUri);
if (cookieHeaders.Length > 0)
{
try
{
container.SetCookies(url, cookieHeaders);
}
catch (CookieException) {}
}
return container;
}
}
private static string GetPageMarkup(string uri)
{
string pageData = null;
using (WebClientEx client = new WebClientEx())
{
pageData = client.DownloadString(uri);
}
return pageData;
}