Skip to content

Instantly share code, notes, and snippets.

@Inndy
Created February 2, 2014 04:59
Show Gist options
  • Save Inndy/8763264 to your computer and use it in GitHub Desktop.
Save Inndy/8763264 to your computer and use it in GitHub Desktop.
Enhanced WebClient class. Cookie, ResponseUri supported.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
namespace SpWebClient
{
public class SpWebClient : WebClient
{
public CookieContainer CookieContainer { get; private set; }
public Uri ResponseUri { get; private set; }
public SpWebClient() : base()
{
this.CookieContainer = new CookieContainer();
this.ResponseUri = null;
}
public SpWebClient(CookieContainer CookieContainer) : base()
{
this.CookieContainer = CookieContainer;
this.ResponseUri = null;
}
public string DownloadString(string Uri, Encoding Encoding)
{
return Encoding.GetString(this.DownloadData(Uri));
}
public string DownloadString(Uri Uri, Encoding Encoding)
{
return Encoding.GetString(this.DownloadData(Uri));
}
protected override WebRequest GetWebRequest(Uri address)
{
WebRequest request = base.GetWebRequest(address);
HttpWebRequest webRequest = request as HttpWebRequest;
if (webRequest != null) webRequest.CookieContainer = this.CookieContainer;
return request;
}
protected override WebResponse GetWebResponse(WebRequest request)
{
WebResponse response = base.GetWebResponse(request);
this.ResponseUri = response.ResponseUri;
return response;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment