在HTTP标头中设置或读取日期(Windows Phone 8) [英] Set or read Date in HTTP header (Windows Phone 8)

查看:111
本文介绍了在HTTP标头中设置或读取日期(Windows Phone 8)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道在Windows Phone 8的HTTP GET请求中设置或读取"Date" HTTP标头的方法吗?

Does anyone knows of a way to set or read the 'Date' HTTP header in an HTTP GET request on Windows Phone 8?

我需要能够设置Date标头的值或至少读取将在请求中实际发送的值.

I need to be able to set the value of the Date header or at least read the value that will actually be sent in the request.

我尝试过类似的操作:

var web_request = HttpWebRequest.CreateHttp(url);
web_request.Headers["Date"] = the_date;

但是这会在运行时产生异常: System.ArgumentException:必须使用适当的属性或方法来修改"Date"标头.

But this produces an exception at run time: System.ArgumentException: The 'Date' header must be modified using the appropriate property or method.

这里有HttpClient的示例代码,但是在Windows Phone 8下显然不可用: 如何设置Content-Type HttpClient请求的标头?

There's sample code here with HttpClient but this is apparently not available under Windows Phone 8: How do you set the Content-Type header for an HttpClient request?

我也尝试过阅读日期,但之后:

I have tried reading the date as well but after:

var web_request = HttpWebRequest.CreateHttp(url);

日期似乎尚未设置.

推荐答案

问题所在:

web_request.Date

是Windows Phone 8版本的HttpWebRequest中没有日期"属性. 请参阅: http://social.msdn.microsoft.com/Forums/windowsapps/zh-CN/5738e95a-5afe-4a49-929d-b51490a5480b/httpwebrequest-date-property-missing

is that there is no "Date" property in the Windows Phone 8 version of HttpWebRequest. See: http://social.msdn.microsoft.com/Forums/windowsapps/en-US/5738e95a-5afe-4a49-929d-b51490a5480b/httpwebrequest-date-property-missing

在此链接中,建议使用HttpClient和HttpRequestMessage.示例:

In this link it is suggested to use HttpClient and HttpRequestMessage. Example:

HttpClient client = new HttpClient();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, url);
request.Headers.Date = DateTime.Now.Subtract(new TimeSpan(10, 0, 0));
HttpResponseMessage response = client.SendAsync(request).Result;
string resultCode = response.StatusCode.ToString();

我之所以陷入困境,是因为HttpClient和HttpRequestMessage似乎也不适用于Windows Phone 8.但是可以添加它们:

I was stuck because HttpClient and HttpRequestMessage seem not to be available for Windows Phone 8 either. But it is possible to add them:

在适用于Windows Phone的Visual Studio(Express)2012中: 工具->库软件包管理器->管理解决方案的NuGet软件包...

In Visual Studio (Express) 2012 for Windows Phone: TOOLS -> Library Package Manager -> Manage NuGet Packages for Solution...

然后搜索"Microsoft HTTP Client Libraries"并安装它. 之后,System.Net.Http可用,并且可以使用HttpClient解决方案.

Then search for "Microsoft HTTP Client Libraries" and install it. After that System.Net.Http is available and the HttpClient solution can be used.

这篇关于在HTTP标头中设置或读取日期(Windows Phone 8)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆