WP7 HttpWebRequest 不带缓存

发布于 2024-10-08 02:52:11 字数 123 浏览 3 评论 0原文

WP7 中的 HttpWebRequest 缓存似乎是默认启用的,如何关闭它? 添加随机数 param url + "?param=" + RND.Next(10000) 有效,但它非常棘手,我不确定它是否有效 与所有服务器。

It seems that HttpWebRequest caching in WP7 is enabled by default, how do I turn it off?
Adding a random
param url + "?param=" + RND.Next(10000) works, but it's quite tricky and I'm not sure if it will work
with all servers.

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

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

发布评论

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

评论(7

七婞 2024-10-15 02:52:12

我发现了 3 种方法

  1. 将随机查询字符串添加到 URI 的末尾(想想 Guid.NewGuid()),这将避免在客户端上缓存,因为查询字符串每次都会不同

字符串 uri = "http://host.com/path?cache="+Guid .NewGuid().ToString();

  1. 在 WCF 服务操作中的 OutgoingResponse 标头中指定无缓存:
var __request = (HttpWebRequest)WebRequest.Create(url.ToString());
if (__request.Headers == null)
    __request.Headers = new WebHeaderCollection();
__request.Headers.Add("缓存控制", "无缓存");
  1. 使用 AspNetCacheProfile 属性标记您的服务操作:
[AspNetCacheProfile("GetContent")]  
公共 ResultABC GetContent(字符串 abc)  
{  
  __request = (HttpWebRequest)WebRequest.Create(abc);
  返回__请求;  
}

并更新你的 web.config

;  
<缓存>  
       
     <输出缓存设置>   
        <输出缓存配置文件>   
            <添加名称=“GetContent”持续时间=“0”noStore=“true”位置=“客户端”varyByParam=“”启用=“true”/>   
           
      
  
...  

I found 3 ways

  1. Add a random Query String to the end of your URI (think Guid.NewGuid()) this will avoid caching on the client as the Query String will be different each time

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

  1. Specify no cache in the OutgoingResponse header within your WCF service operation:
var __request = (HttpWebRequest)WebRequest.Create(url.ToString());
if (__request.Headers == null)
    __request.Headers = new WebHeaderCollection();
__request.Headers.Add("Cache-Control", "no-cache");
  1. markup your service operation with the AspNetCacheProfile attribute:
[AspNetCacheProfile("GetContent")]  
public ResultABC GetContent(string abc)  
{  
  __request = (HttpWebRequest)WebRequest.Create(abc);
  return __request;  
}

And update your web.config

<system.web>  
<caching>  
     <outputCache enableOutputCache="true" />  
     <outputCacheSettings>   
        <outputCacheProfiles >   
            <add name="GetContent" duration="0" noStore="true" location="Client" varyByParam="" enabled="true"/>   
        </outputCacheProfiles>   
    </outputCacheSettings>  
</caching>  
...  
</system.web>
许仙没带伞 2024-10-15 02:52:12

添加随机数还不错,而且会起作用。我已经使用了 Time (在 ajax 调用中)。像文件夹一样放在url中。

Adding random number is not bad and it will work. I have used Time (in ajax call). Was placed in the url like a folder.

迷乱花海 2024-10-15 02:52:12

是的,这是可能的......:)我花了一周的实验,答案非常简单:

      HttpWebRequest _webRequest = WebRequest.CreateHttp(_currentUrl);

     _webRequest.AllowReadStreamBuffering = false
 _webRequest.BeginGetResponse(_onDownload,
 userState);

Yes is possible... :) I spend one week of Experiment and the answer is really simple :

      HttpWebRequest _webRequest = WebRequest.CreateHttp(_currentUrl);

     _webRequest.AllowReadStreamBuffering = false
 _webRequest.BeginGetResponse(_onDownload,
 userState);
惯饮孤独 2024-10-15 02:52:11

为了将来的参考,这对我有用(由于项目要求,我无法使用额外的查询参数):

        HttpWebRequest request = HttpWebRequest.CreateHttp(url);
        if (request.Headers == null)
        {
            request.Headers = new WebHeaderCollection();
        }
        request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();

For future reference , this worked for me ( I could not use additional query parameter due to project requirements) :

        HttpWebRequest request = HttpWebRequest.CreateHttp(url);
        if (request.Headers == null)
        {
            request.Headers = new WebHeaderCollection();
        }
        request.Headers[HttpRequestHeader.IfModifiedSince] = DateTime.UtcNow.ToString();
萌能量女王 2024-10-15 02:52:11

对于 HttpClient(适用于 Windows Phone 的便携式),服务器端的“Cache-Control”:“no-cache”仅有时有效。我也无法将查询字符串随机值添加到 RESTful api 调用中。

@frno 的解决方案效果很好,看起来像 HttpClient:

client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;

谢谢。

In case of HttpClient (Portable for Windows Phone) "Cache-Control": "no-cache" on server side works only sometimes. And I cannot add query string random value to RESTful api call as well.

Solution from @frno works good and looks like for HttpClient:

client.DefaultRequestHeaders.IfModifiedSince = DateTime.UtcNow;

Thank you.

甜扑 2024-10-15 02:52:11

您如何知道正在缓存的是手机,而不是服务器(或中间的代理)?
您是否使用 Fiddler2(或同等版本)检查过这一点?

您是否尝试过设置标头来禁用缓存?
像这样的东西:

myRequest = (HttpWebRequest)WebRequest.Create(myUri);

myRequest.Headers["Cache-Control"] = "no-cache";
myRequest.Headers["Pragma"] = "no-cache";

How do you know it's the phone, not the server (or a proxy somewhere between) which is caching?
Have you checked this with Fiddler2 (or equivalent)?

Have you tried setting headers to disable caching?
Something like:

myRequest = (HttpWebRequest)WebRequest.Create(myUri);

myRequest.Headers["Cache-Control"] = "no-cache";
myRequest.Headers["Pragma"] = "no-cache";
半仙 2024-10-15 02:52:11

我们在 Chrome 中托管的 Silverlight 中看到了相同的行为。

如果我们想防止缓存,我们可以在请求 URL 中添加 "?nocache=" + DateTime.Now.Ticks.ToString()

We've seen the same behaviour with Silverlight hosted in Chrome.

We add a "?nocache=" + DateTime.Now.Ticks.ToString() to our request URLs if we want to prevent caching.

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