3

如果我使用 HttpListener,如何将 WebException 304 错误模拟回浏览器?

那就是我收到了对我的 HttpListener 的请求,然后获得了 HttpListenerContext,那么从这一点开始,我将如何模仿/安排 HTTP“304 Not Modified”响应通过 HttpListenerContext.response 有效地发送回浏览器?

编辑:

我尝试了以下操作,但是尝试将 WebException.Status 复制到 HttpWebResponse.StatusCode 时出现错误(状态代码必须恰好是三位数)。关于如何纠正这个问题的任何想法?

    catch (WebException ex)
    {
        listenerContext.Response.StatusCode = (int)ex.Status;   //ERROR: The status code must be exactly three digits
        listenerContext.Response.StatusDescription = ex.Message;
        listenerContext.Response.Close();

谢谢

4

1 回答 1

2

我想我有它:

    catch (WebException ex)
    {


        if (ex.Status == WebExceptionStatus.ProtocolError)
        {
            int statusCode = (int) ((HttpWebResponse) ex.Response).StatusCode;
            listenerContext.Response.StatusCode = statusCode;
            listenerContext.Response.StatusDescription = ex.Message;
            log("WARNING", uri, "WebException/ProtocolError: " + ex.GetType() + " - " + ex.Message);
        }
        else
        {
            log("ERROR", uri, "WebException - " + ex.GetType() + " - " + ex.Message);

        }

        listenerContext.Response.Close();
    }
于 2010-04-11T04:29:11.567 回答