简体   繁体   中英

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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