Skip to content

Instantly share code, notes, and snippets.

@Layoric
Created December 2, 2012 06:00
Show Gist options
  • Save Layoric/4187159 to your computer and use it in GitHub Desktop.
Save Layoric/4187159 to your computer and use it in GitHub Desktop.
CookieAwareWebClient
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
namespace HoNForums
{
public class CookieAwareWebClient : WebClient
{
[System.Security.SecuritySafeCritical]
public CookieAwareWebClient()
: base()
{
}
private static CookieAwareWebClient singletonWebClient;
public static CookieAwareWebClient SingletonWebClient
{
get
{
if (singletonWebClient == null)
{
singletonWebClient = new CookieAwareWebClient();
}
return singletonWebClient;
}
}
public CookieContainer CookieJar = new CookieContainer();
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
if (request is HttpWebRequest)
{
if (CookieJar == null)
CookieJar = new CookieContainer();
(request as HttpWebRequest).CookieContainer = CookieJar;
}
return request;
}
protected override WebResponse GetWebResponse(WebRequest request, IAsyncResult result)
{
WebResponse response = base.GetWebResponse(request, result);
if (response is HttpWebResponse)
{
if (CookieJar == null)
CookieJar = new CookieContainer();
CookieJar.Add(request.RequestUri, (response as HttpWebResponse).Cookies);
}
return response;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment