Set or read Date in HTTP header (Windows Phone 8)

2019-09-05 14:09发布

问题:

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

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.

I have tried with something like:

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

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

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

The date does not seem to be set yet.

回答1:

That worked well after adding "Microsoft HTTP Client Libraries". However there is still a problem in this library on WindowsPhone Platform.

PROBLEM:

I have a scenario where i want to add a "Date" header in a specific format, so i used

string customDate = "11/29/2013 7:46:25"

DefaultRequestHeaders.TryAddWithoutValidation("Date", customDate);

the above line adds the "Date" header, that means it does not throw any exception, but when i checked in fidler there is no "Date" header added. The same code works well in Windows8 store apps.

Looks like there is a bug in "Microsoft HTTP Client Libraries" for WindowsPhone8 platform.



回答2:

By using "Microsoft HTTP Client Libraries" for Date header works well and good if Date is of DateTime object, however if i want to assign a value to Date header in a specific format as mentioned in earlier post then it does not get added.



回答3:

Its a header with built-in support that you need to set/get explicitly;

web_request.Date = DateTime.UtcNow;

(Its default is 01/01/0001 00:00:00 which is not sent in the request)



回答4:

The problem with:

web_request.Date

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

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

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

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

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