No cache with HttpClient in Windows Phone 8

后端 未结 5 388
夕颜
夕颜 2020-12-03 18:40

I\'ve read that in order to disable caching while using get and post methods in HttpClient, I need to use a WebRequestHandler as my HttpClien

5条回答
  •  無奈伤痛
    2020-12-03 19:19

    It's not possible to reference regular .NET assemblies in a Windows Phone 8 project. You can only use the .NET API for Windows Phone. This is a subset of regular .NET. See http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj207211%28v=vs.105%29.aspx for more info.

    The default caching of HttpClient (and HttpWebRequest) can be worked around by appending a value to the query string. For example, a guid.

    string uri = "http://host/path?cache=" + Guid.NewGuid().ToString();
    

    A better solution, like pointed out in the comment above, is to set the "If-Modified-Since" header. HttpWebRequest has it built in:

    HttpWebRequest request = HttpWebRequest.CreateHttp(url);
    if (request.Headers == null)
        request.Headers = new WebHeaderCollection();
    // Make sure that you format time string according RFC.
    // Otherwise setting header value will give ArgumentException for culture like 'ti-ER'
    request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString("r"); 
    

    But you could add the header manually using an HttpClient I guess.

提交回复
热议问题