Wednesday, September 26, 2012

How to set http header If-Modified-Since on a WebRequest in Windows Phone 7?

How to set http header If-Modified-Since on a WebRequest in Windows Phone 7?

Trying to set the 'If-Modified-Since' header in wp7:

request.Headers[HttpRequestHeader.IfModifiedSince] = dateString; 

Gives the error:

This header must be modified with the appropriate property.

Which means that the property .IsModifiedSince should be used on the request class, as described in MSDN: http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.ifmodifiedsince.aspx

But this property does not exist in Silverlight i.e. WP7.

Has anyone been able to set this header for a http request on Windows Phone 7?

Shawn Wildermuth posted this problem back in September 2010, with no solution: http://social.msdn.microsoft.com/Forums/en/windowsphone7series/thread/8aec7481-2cf3-4047-b0d4-05d4313a9e4c

Thank you!

Answers & Comments...

Answer: 1
request.Headers.Add("If-Modified-Since", datestring); 
by : Chris Kookenhttp://stackoverflow.com/users/171742

Answer: 2

You can just use the string that HttpRequestHeader.IfModifiedSince represents:

request.Headers["If-Modified-Since"] = dateString;  

I've had to do this with a number of other headers which WP7 doesn't expose helper methods for setting.

Update
Based on the remarks at http://msdn.microsoft.com/en-us/library/8y7x3zz2(v=VS.95).aspx it would appear that it is not possible to set this header in WP7.

As an alternative you could create your own proxy server to handle the caching for your app.

by : Matt Laceyhttp://stackoverflow.com/users/1755

Answer: 3

The short answer is: It cannot be done, not supported.

Solution would be, as Matt Lacey states, to create a proxy class to handle this.

That proxy would set

request.AllowStreamReadBuffering = false; 

and then parse the response until the header ends or the header value has been found.

Note! This workaround limits the data downloaded to the phone, but not the work needed by the server to process the request.

by : andyhammarhttp://stackoverflow.com/users/107902

Answer: 4

This can only be set on the HTTPWebRequest object so casting the WebRequest should allow you to set the property eg:

((HttpWebRequest)request).IfModifiedSince = modifiedDate; 

It takes a DateTime object so you may need to parse the string first.

by : I draw a line when I'm Deadhttp://stackoverflow.com/users/1699810




No comments:

Post a Comment

Send us your comment related to the topic mentioned on the blog