当我在 C# 中发出 WebRequest 时,为什么未设置 Date 标头?

发布于 2024-09-06 20:15:24 字数 871 浏览 10 评论 0原文

今晚,我开始了一个小项目,尝试创建一个 C# 库来访问 Google存储API。当您向 Google Storage API 创建任何类型的请求时,您必须在网络请求中包含“日期”标头。

我尝试用 C# 创建一个 WebRequest,并注意到我无法手动设置“Date”标头。根据 此 MSDN 页面,Date 属性应该由系统自动设置。

由于缺少“日期”标头,我向 Google 发出的请求失败了。 Fiddler 确认我的请求中未发送日期标头。

这是我正在使用的代码片段:

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();

知道会发生什么吗?为什么我的 Web 请求不发送日期标头?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

π浅易 2024-09-13 20:15:24

根据文档, HttpWebRequest.Date 属性默认为 DateTime.MinValue,它不会随请求一起发送 Date 标头。

因此,要设置它,您需要做的就是......

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

webRequest.Date = DateTime.UtcNow;

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;
記柔刀 2024-09-13 20:15:24

我能够通过使用 TcpClient 类并创建自己的标头来解决此问题,如本文所述:
http://www.issociate.de/board/goto/970687/HTTPWebRequest_Date_Header。 html

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

奢欲 2024-09-13 20:15:24

除了使用 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);

Other than using the tcp class and recode everything would be to use reflections to add them with the method "AddWithoutValidation". This will work in .Net 3.5 - a workaround

//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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文