使用HttpWebRequest时,为什么我在某些链接上出现“(304)Not Modified”错误?

时间:2010-04-08 21:30:34

标签: http httpwebrequest http-status-code-304 if-modified-since

我尝试使用HttpWebRequest访问某些链接的任何想法我得到“远程服务器返回错误:(304)未修改。”在代码?

我使用的代码来自Jeff's post here(页面似乎已消失,请参阅archive copy at the Wayback Machine)。

请注意,代码的概念是一个简单的代理服务器,所以我将浏览器指向这个本地运行的代码片段,它会获取我的浏览器请求,然后通过创建一个新的HttpWebRequest代理它,就像你'我会在代码中看到。它适用于大多数网站/链接,但对于一些人来说,这个错误出现了。您将在代码中看到一个关键位,它似乎将http标头设置从浏览器请求复制到它的请求到网站,并在标头属性中复制。不确定问题是否与它如何模仿请求的这个方面有关,然后结果又会发生什么?

case "If-Modified-Since":
   request.IfModifiedSince = DateTime.Parse(listenerContext.Request.Headers[key]);
   break;

我从http://en.wikipedia.org/wiki/Main_Page

中得到了问题

PS。在此更新

仍然无法解决这个问题。基本上我可以识别1个有问题的链接,它似乎工作正常,第二次得到错误,第3次OK,第4次得到错误,第5次OK等等。好像有一些状态没有被清除或代码中有些东西。我试图使用“使用”类型语句等来清理代码。

这是代码。如果有人能够通过这个代理代码每次第二次浏览到http://newsimg.bbc.co.uk/css/screen/1_0_16/nol/v4/story.css之类的链接(从第二次开始,而不是第一次),我就会发现我想听到的错误。

class Program
{
    static void Main(string[] args)
    {
        Proxy p = new Proxy(8080);

        Thread proxythread = new Thread(new ThreadStart(p.Start));
        proxythread.Start();

        Console.WriteLine("Proxy Started. Press Any Key To Stop...");
        Console.ReadKey();

        p.Stop();
     }
}

public class Proxy
{
    private HttpListener _listener;
    private int _port;

    public Proxy(int port)
    {
        int defaultport = 8080;

        // Setup Thread Pool
        System.Threading.ThreadPool.SetMaxThreads(50, 1000);
        System.Threading.ThreadPool.SetMinThreads(50, 50);

        // Sanitize Port Number
        if (port < 1024 || port > 65535)
            port = defaultport;

        // Create HttpListener Prefix
        string prefix = string.Format("http://*:{0}/", port);
        _port = port;

        // Create HttpListener
        _listener = new HttpListener();
        _listener.Prefixes.Add(prefix);
    }

    public void Start()
    {
        _listener.Start();

        while (true)
        {
            HttpListenerContext request = null;

            try
            {
                request = _listener.GetContext();

                // Statistics (by Greg)
                int availThreads = -1;
                int compPortThreads = -1;
                ThreadPool.GetAvailableThreads(out availThreads, out compPortThreads);
                log("INFO", request.Request.Url.ToString(), "START - [" + availThreads + "]");

                ThreadPool.QueueUserWorkItem(ProcessRequest, request);
            }
            catch (HttpListenerException ex)
            {
                log("ERROR", "NA", "INFO: HttpListenerException - " + ex.Message);
                break;
            }
            catch (InvalidOperationException ex)
            {
                log("ERROR", "NA", "INFO: InvalidOperationException - " + ex.Message);
                break;
            }
        }
    }

    public void Stop()
    {
        _listener.Stop();
    }

    private void log(string sev, string uri, string message)
    {
        Console.Out.WriteLine(Process.GetCurrentProcess().Id + " - " + sev + " (" + uri + "): " + message);
    }

    private void ProcessRequest(object _listenerContext)
    {
        #region local variables
        HttpWebRequest psRequest;                   // Request to send to remote web server
        HttpWebResponse psResponse;                 // Response from remote web server         
        List<byte> requestBody = new List<byte>();  // Byte array to hold the request's body
        List<byte> responseBody = new List<byte>(); // Byte array to hold the response's body
        byte[] buffer;
        string uri = "";
        #endregion

        var listenerContext = (HttpListenerContext)_listenerContext;
        uri = listenerContext.Request.Url.ToString().Replace(string.Format(":{0}", _port), "");

        // Create Interent Request 
        HttpWebRequest internetRequest = (HttpWebRequest)WebRequest.Create(uri);
        #region Build Request Up
        internetRequest.Method = listenerContext.Request.HttpMethod;
        internetRequest.ProtocolVersion = listenerContext.Request.ProtocolVersion;
        internetRequest.UserAgent = listenerContext.Request.UserAgent;
        foreach (string key in listenerContext.Request.Headers.AllKeys)
        {
            try
            {
                switch (key)
                {
                    case "Proxy-Connection":
                    case "Connection":
                        internetRequest.KeepAlive = (listenerContext.Request.Headers[key].ToLower() == "keep-alive") ? true : false;
                        break;

                    case "Content-Length":
                        internetRequest.ContentLength = listenerContext.Request.ContentLength64;
                        break;

                    case "Content-Type":
                        internetRequest.ContentType = listenerContext.Request.ContentType;
                        break;

                    case "Accept":
                        internetRequest.Accept = listenerContext.Request.Headers[key];
                        break;

                    case "Host":
                        break;

                    case "Referer":
                        internetRequest.Referer = listenerContext.Request.Headers[key];
                        break;

                    case "If-Modified-Since":
                        internetRequest.IfModifiedSince = DateTime.Parse(listenerContext.Request.Headers[key]);
                        break;

                    default:
                        internetRequest.Headers.Add(key, listenerContext.Request.Headers[key]);
                        break;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error settup up psRequest object. Error = " + ex.Message + "\n" + ex.StackTrace);
            }
        }
        #endregion

        #region Copy content into request
        buffer = new byte[1024];
        using (Stream instream = listenerContext.Request.InputStream)
        {
            int incount = instream.Read(buffer, 0, buffer.Length);
            while (incount > 0)
            {
                internetRequest.GetRequestStream().Write(buffer, 0, incount);
                incount = instream.Read(buffer, 0, buffer.Length);
            }
        }
        #endregion

        // Get Internet Response
        HttpWebResponse internetResponse = null;
        try
        {
            using (internetResponse = (HttpWebResponse)internetRequest.GetResponse())
            {
                #region Configure Local Response Header Keys
                foreach (string key in internetResponse.Headers.Keys)
                {
                    try
                    {
                        switch (key)
                        {
                            case "Transfer-Encoding":
                                listenerContext.Response.SendChunked = (internetResponse.Headers[key].ToLower() == "chunked") ? true : false;
                                break;

                            case "Content-Length":
                                listenerContext.Response.ContentLength64 = internetResponse.ContentLength;
                                break;

                            case "Content-Type":
                                listenerContext.Response.ContentType = internetResponse.Headers[key];
                                break;

                            case "Keep-Alive":
                                listenerContext.Response.KeepAlive = true;
                                break;

                            default:
                                listenerContext.Response.Headers.Add(key, internetResponse.Headers[key]);
                                break;
                        }
                    }
                    catch (Exception ex)
                    {
                        log("ERROR", uri, "Error settup up listenerContext.Response objects. Error = " + ex.Message + "\n" + ex.StackTrace);
                    }
                }
                #endregion

                try
                {
                    // Transfer the body data from Internet Response to Internal Response
                    buffer = new byte[1024];
                    using (Stream inputStream = internetResponse.GetResponseStream())
                    {
                        int outcount = inputStream.Read(buffer, 0, buffer.Length);
                        while (outcount > 0)
                        {
                            listenerContext.Response.OutputStream.Write(buffer, 0, outcount);
                            outcount = inputStream.Read(buffer, 0, buffer.Length);
                        }
                    }
                }
                catch (Exception ex)
                {
                    log("ERROR", uri, "Could not obtain response from URI: " + ex.Message);
                }
                finally
                {
                    listenerContext.Response.OutputStream.Close();
                }
            }
        }
        catch (Exception ex)
        {
            //if (ex is InvalidOperationException ||
            //    ex is ProtocolViolationException ||
            //    ex is WebException)
            //{
            //    log(uri, "Could not successfully get response: " + ex.GetType() + " - " + ex.Message);
            //    listenerContext.Response.Close();
            //    return;
            //}
            //else { throw; }

            log("ERROR", uri, "Could not successfully get response: " + ex.GetType() + " - " + ex.Message);
            listenerContext.Response.Close();
        }
    }
}

这是我看到的一个例子 - 第一次打击很好,第二次有错误......

Proxy Started. Press Any Key To Stop...
2080 - INFO (http://newsimg.bbc.co.uk:8080/css/screen/1_0_16/nol/v4/story.css): START - [50]
2080 - INFO (http://newsimg.bbc.co.uk:8080/css/screen/1_0_16/nol/v4/story.css): START - [50]
2080 - ERROR (http://newsimg.bbc.co.uk/css/screen/1_0_16/nol/v4/story.css): Could not successfully get response: System.Net.WebException - The remote server returned an error: (304) Not Modified.

5 个答案:

答案 0 :(得分:81)

首先,这不是错误。 3xx表示重定向。实际错误是4xx(客户端错误)和5xx(服务器错误)。

如果客户获得304 Not Modified,那么客户有责任从自己的缓存中显示有问题的资源。通常,代理不应该担心这一点。这只是信使。

答案 1 :(得分:19)

这是预期的行为。

当您发出HTTP请求时,服务器通常会返回代码200 OK。如果您设置If-Modified-Since,服务器可能会返回304 Not modified(并且响应将不包含内容)。这应该是您没有修改页面的提示。

304 If-Modified-Since应被视为错误并抛出异常。现在,每次尝试使用{{1}}时都必须通过捕获异常来清理它们。

答案 2 :(得分:14)

F5 并不总是有效。

<强>为什么吗

因为您的ISP也在为您缓存网页数据。

解决方案:强制刷新。

在Firefox或Chrome中按 CTRL + F5 强制刷新浏览器以清除ISP缓存,而不是仅按 F5

然后,您可以在浏览器 F12 开发人员工具网络标签中看到200响应而不是304响应。

另一个技巧是在请求页面的URL字符串末尾添加问号?

http://localhost:52199/Customers/Create?

问号将确保浏览器刷新请求而不缓存任何先前的请求。

此外,在 Visual Studio 中,您可以在隐身模式下将默认浏览器设置为 Chrome ,以避免在开发过程中出现缓存问题,方法是添加Chrome隐身模式作为默认浏览器,请参阅步骤(自我说明):

Go to browsers list Select browse with... Click Add... Point to the chrome.exe on your platform, add argument "Incognito" Choose the browser you just added and set as default, then click browse

答案 3 :(得分:0)

这不是问题,因为缓存...

要克服这个问题,请在端点调用中添加时间戳,例如axios.get('/api/products')

时间戳后应该是 axios.get(/api/products?${Date.now()}

它将解析您的304 状态代码。

答案 4 :(得分:-3)

我认为您尚未安装这些功能。见下图。

enter image description here

前几天我也遇到了这个问题。安装此功能后,我解决了它。如果您尚未安装此功能,请安装它。

安装流程:

  1. 转到android studio
  2. 工具
  3. 的Android
  4. SDK Manager
  5. 外观&amp;行为
  6. Android SDK