views:

36

answers:

2

Tonight I started a small project trying to create a C# library to access the Google Storage API. When you create any kind of request to the Google Storage API, you have to include the "Date" header in the web request.

I tried to create a WebRequest in C#, and noticed that I couldn't set the "Date" header manually. According to this MSDN page, the Date property should be getting set automatically by the system.

My requests to Google are failing though due to a missing "Date" header. Fiddler confirms that a Date header is not being sent in my request.

Here is the code snippet I'm using:

WebRequest webRequest;
webRequest = WebRequest.Create("http://commondatastorage.googleapis.com");

String auth = "GOOG1 " + m_accessKey + ":" + CreateSignature();

webRequest.Headers.Add("Authorization", auth);
webRequest.ContentType = "text/html";

Stream objStream;
objStream = webRequest.GetResponse().GetResponseStream();

Any idea what could be going on? Why isn't the Date header being sent with my web request?

A: 

According to the documentation, the HttpWebRequest.Date property is DateTime.MinValue by default which will not send the Date header with the request.

So to set it all you should have to do is...

HttpWebRequest webRequest;
webRequest = (HttpWebRequest)WebRequest.Create("http://commondatastorage.googleapis.com");

webRequest.Date = DateTime.UtcNow;
Josh Einstein
I think this property can only be set if you're targetting .NET 4.0:http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.date.aspxI definitely can't set this property when targetting .NET 3.5.
dreadpirateryan
A: 

I was able to solve this by using the TcpClient class, and creating my own headers, as outlined in this post: http://www.issociate.de/board/goto/970687/HTTPWebRequest_Date_Header.html

dreadpirateryan