Why is the Date header not set when I do WebRequest in C #?

Today I started a small project trying to create a C # library to access the Google Storage API . When you make any request to the Google Storage API, you must include the Date header in the web request.

I tried creating a WebRequest in C # and noticed that I cannot set the Date header manually. According to this MSDN page , the Date property should be automatically set by the system.

My requests to Google are not fulfilled, although due to the lack of a Date heading. Fiddler confirms that the date header is not 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();

, ? Date -?

+3
3

, HttpWebRequest.Date DateTime.MinValue , Date .

, , , ...

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

webRequest.Date = DateTime.UtcNow;
+1

tcp , , "AddWithoutValidation". .Net 3.5 -

//request.Headers.Add("date", getIsoStringFromDate(DateTime.Now)); <- EXCEPTION in .Net 3.5

 //WORKAROUND
 Type type = request.Headers.GetType();
 BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
 MethodInfo methodInfo = type.GetMethod("AddWithoutValidate", flags);
 object[] myPara = new object[2];
 myPara[0] = "date";
 myPara[1] = DateTime.Now.ToShortDateString();
 methodInfo.Invoke(request.Headers, myPara);
0
source

Source: https://habr.com/ru/post/1751332/


All Articles