HttpListener-如何发送WebException HTTP 304“未修改”错误返回浏览器? [英] HttpListener - how do I send a WebException HTTP 304 "Not Modified" error back to browser?

查看:211
本文介绍了HttpListener-如何发送WebException HTTP 304“未修改”错误返回浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I mimic a WebException 304 error back to browser if I am using HttpListener?

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

That is I have received a request to my HttpListener, and then obtained the HttpListenerContext, then from this point how would I mimic/arrange for a HTTP "304 Not Modified" response to be effectively sent back to the browser via the HttpListenerContext.response?

编辑:

我尝试了以下操作,但是在将WebException.Status复制到HttpWebResponse时遇到错误。 StatusCode(状态码必须为三位数)。有关如何纠正此问题的任何想法?

I tried the following however I get an error trying to copy WebException.Status to HttpWebResponse.StatusCode (The status code must be exactly three digits). Any ideas on how to correct this?

    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();

谢谢

推荐答案

我认为我有:

    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();
    }

这篇关于HttpListener-如何发送WebException HTTP 304“未修改”错误返回浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆