如果我使用 HttpListener,如何将 WebException 304 错误模拟回浏览器?
也就是说,我收到了对 HttpListener 的请求,然后获得了 HttpListenerContext,那么从现在开始,我将如何模仿/安排 HTTP“304 Not Modified”响应通过 HttpListenerContext.response 有效发送回浏览器?
EDIT:
我尝试了以下操作,但是在尝试将 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();
thanks
我想我有:
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();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)