简体   繁体   English

C#-为什么我的HTTPWebRequest每次都返回一个缓存的响应?

[英]C# - Why is my HTTPWebRequest returning a cached response every time?

I've seen various similar questions to this, but nothing has helped solve my issue. 我已经看到各种类似的问题,但是没有任何事情可以解决我的问题。 Here's a code fragment with which I'm working: 这是我正在使用的代码片段:

NetworkCredential credentials = new NetworkCredential(CredentialsManager.Username, CredentialsManager.Password);

HttpWebRequest getReq = (HttpWebRequest) WebRequest.Create(m_editPageUrl);
getReq.CachePolicy = new RequestCachePolicy(RequestCacheLevel.BypassCache); // I've also tried RequestCacheLevel.NoCacheNoStore
getReq.Credentials = credentials;
getReq.Timeout = 1000;
getReq.Method = "GET";
getReq.Accept = "text/html";

string responseString;
using (HttpWebResponse getResponse = (HttpWebResponse) getReq.GetResponse())
{
    using (Stream responseStream = getResponse.GetResponseStream())
    {
        if (responseStream == null)
            throw new Exception("Did not receive a response from the specified page.");

        using (StreamReader reader = new StreamReader(responseStream))
            responseString = reader.ReadToEnd();
    }
}

For some reason, whatever gets stored into responseString is being cached, even though I've told the HTTPWebRequest to bypass the cache. 由于某种原因,即使我已告诉HTTPWebRequest绕过缓存,存储在responseString中的任何内容也会被缓存。 About every hour, I get a newer response, but then if I change anything, the newer response (which should now be invalidated since there's an even newer version) is still passed to me. 大约每个小时,我都会收到更新的响应,但是如果我更改了任何内容,更新的响应(由于有更新的版本,现在应该无效)仍然传递给我。 I've been told on good authority that the server is set up to not cache responses, so I must be doing something wrong. 有人告诉我,服务器已设置为不缓存响应,因此我必须做错了。 I just can't figure out what. 我只是不知道是什么。

I read somewhere that it may help to make a more low-level loader using sockets. 我在某处读到它可能有助于使用套接字制作更底层的加载器。 However, I couldn't find anywhere that easily showed how to do this. 但是,我找不到任何容易说明如何执行此操作的地方。 If this is what I should do, please let me know where to look to find help on that. 如果这是我应该做的,请告诉我在哪里可以寻求帮助。

Thanks. 谢谢。

While doing some digging in the code for the site hosted on the server my application accesses, I found out that the site has a cache service which it maintains. 在对应用程序访问的服务器上托管的站点的代码进行一些挖掘时,我发现该站点具有要维护的缓存服务。 Once this was disabled, everything ran like a charm. 一旦禁用此功能,一切都会像魅力一样。 My original code works fine. 我的原始代码工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM