Archive

Archive for October, 2020

Overriding the Date in a WebClient Request in C#

The WebClient object in C# greatly simplifies the actions of making HTTP GET and POST requests, however, when you need to edit an “Under the hood” property, then you find that WebClient has hidden this functionality from you.

In this example, I want to override the Date property of the HTTP request, effectively sending a request to the server, but saying it was sent at a different time. This is not a property you often want to mess with, but you can do it as follows;

private class SpecializedWebClient : WebClient
{
    public DateTime Date { get; set; }
    protected override WebRequest GetWebRequest(Uri address)
    {
	var request = base.GetWebRequest(address) as HttpWebRequest;
	if (request != null) request.Date = Date;
	return request;
    }
}

I hope someone finds this useful!

Categories: Uncategorized