我正在编写一个上传函数,并且httpRuntime在web.config中使用大于指定最大大小的文件时捕获"System.Web.HttpException:超出最大请求长度"时遇到问题(最大大小设置为5120).我正在使用一个简单<input>的文件.
问题是在上传按钮的click-event之前抛出了异常,并且异常发生在我的代码运行之前.那么如何捕获和处理异常呢?
编辑:异常立即抛出,所以我很确定它不是由于连接速度慢导致的超时问题.
当我调用时,Response.Redirect(someUrl)我得到一个HttpException:"在发送HTTP头之后无法重定向".
为什么我这样做?我该如何解决这个问题?
我可能在这里遗漏了一些明显的东西.
我正在使用Message字符串中包含的HttpClient哪个throws .HttpRequestExceptionStatusCode
我该如何访问StatusCode?
编辑:更多信息,我急着写这个问题.
我正在使用HttpClient我的WebApi项目中的另一个API.是的,我知道我为什么打电话EnsureSuccessStatusCode().我想在下游传播一些错误,例如404和403.
我想要的只是不断变换HttpRequestException为HttpResponseException使用自定义ExceptionFilterAttribute.
不幸的是,HttpRequestException除了消息之外,我没有携带任何额外的信息.我希望以StatusCode原始(int或enum)形式发现.
看起来我可以:
在MVC 5中,您可以使用HTTP代码抛出HttpException,这将设置响应,如下所示:
throw new HttpException((int)HttpStatusCode.BadRequest, "Bad Request.");
Run Code Online (Sandbox Code Playgroud)
ASP.NET 5/MVC 6中不存在HttpException.等效代码是什么?
在StackOverflow上,我们每天都会看到一些"请求超时"异常.
事实:
我们已经测试了基于服务器的超时(即使用Thread.Sleep),并且在异常日志中正确捕获了所有表单变量 - 这使我们相信客户端在分配的时间内发送请求时遇到问题.
关于如何捕获/调试这种情况的任何想法都是非常受欢迎的!
我写了一个自定义的http处理程序.我通过编写实现IHttphandler的类来完成此操作.
在那个班级里面,我有这样的代码,
context.Response.Clear();
context.Response.ClearHeaders();
context.Response.AddHeader("Content-Disposition", "attachment;filename=" + attachmentFileName);
context.Response.AddHeader("Content-Length", new FileInfo(downloadFile).Length.ToString());
context.Response.ContentType = GetMimeType(attachmentFileName);
context.Response.TransmitFile(downloadFile);
context.Response.Flush();
context.Response.Close();
Run Code Online (Sandbox Code Playgroud)
偶尔我会收到这样的错误,
Exception HttpException The remote host closed the connection The error code is 0x800703E3
Run Code Online (Sandbox Code Playgroud)
或这个,
Exception HttpException The remote host closed the connection The error code is 0x80070040
Run Code Online (Sandbox Code Playgroud)
在这两种情况下,堆栈跟踪都是这样的,
at System.Web.Hosting.IIS7WorkerRequest.RaiseCommunicationError(Int32 result, Boolean throwOnDisconnect)
at System.Web.Hosting.IIS7WorkerRequest.ExplicitFlush()
at System.Web.HttpResponse.Flush(Boolean finalFlush)
at System.Web.HttpResponse.Flush()
Run Code Online (Sandbox Code Playgroud)
这种情况发生在生产中,如果我回顾过去几天错误发生了23次,总共上述代码被称为497次.
我怀疑这个失败与用户点击链接不止一次启动上述代码(这将给他们多个下载对话框)有关,然后他们取消了其中一些.话虽如此,如果它是这样的,我会期望连接在两端优雅地关闭.
如何证明此错误的确切原因?我试图启用这样的.NET跟踪为什么不跟踪侦听器记录自定义处理程序流量?但无法让它发挥作用.
我发现虽然我已启用IIS跟踪来记录失败的请求.失败再次发生,并且该日志中没有任何内容.
我可以启用任何其他跟踪吗?
接下来我尝试的是这个,
if (context.Response.IsClientConnected)
{
context.Response.Flush();
context.Response.Close();
}
else
{
LogMessage("Client has disconnected before flush was …Run Code Online (Sandbox Code Playgroud) 我在IIS 7共享托管环境中有一个网站.它运行的是.NET 3.5.我有一个下载按钮从服务器下载文件.
当我将此应用程序本地部署到IIS 6时,它运行正常.在IIS 7共享主机服务器上,发生异常.
句柄无效.(来自HRESULT的异常:0x80070006(E_HANDLE))描述:在执行当前Web请求期间发生了未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
System.Runtime.InteropServices.COMException:句柄无效.(来自HRESULT的异常:0x80070006(E_HANDLE))
COMException(0x80070006):句柄无效.(来自HRESULT的异常:0x80070006(E_HANDLE))] [HttpException(0x80004005):与远程主机通信时发生错误.错误代码是0x80070006.]
怎么解决这个问题?
string strRequest = Convert.ToString(Request.QueryString.Get("file"));
System.IO.FileInfo file = new System.IO.FileInfo(strRequest);
if (file.Exists)
{
Response.Clear();
Response.ContentType = ReturnExtension(System.IO.Path.GetExtension(file.Name));
Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.TransmitFile(strRequest);
Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
//DownloadFile(file.FullName, file.Name);
}
Run Code Online (Sandbox Code Playgroud) 我有一个视图使用响应BinaryWrite方法呈现流.这在使用Beta 2的ASP.NET 4下工作正常,但在RC版本中引发了这个异常:
"HttpException","使用自定义TextWriter时,OutputStream不可用."
<%@ Page Title="" Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="System.IO" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (ViewData["Error"] == null)
{
Response.Buffer = true;
Response.Clear();
Response.ContentType = ViewData["DocType"] as string;
Response.AddHeader("content-disposition", ViewData["Disposition"] as string);
Response.CacheControl = "No-cache";
MemoryStream stream = ViewData["DocAsStream"] as MemoryStream;
Response.BinaryWrite(stream.ToArray());
Response.Flush();
Response.Close();
}
}
</script>
</script>
Run Code Online (Sandbox Code Playgroud)
视图是从客户端重定向生成的(使用Url.Action帮助程序在前一页中使用jquery替换位置调用来渲染链接).这都在iframe中.
任何人都知道为什么会这样?
我正在尝试在客户端返回HTTP 403错误代码.我已经读过HttpException是实现这一目标最干净的方法,但它对我不起作用.我从这样的页面中抛出异常:
throw new HttpException(403,"You must be logged in to access this resource.");
Run Code Online (Sandbox Code Playgroud)
但是,当CustomErrors关闭时,这只会给出标准的ASP.Net堆栈跟踪(500错误).如果启用了CustomErrors,则不会重定向到我设置为403错误发生时显示的页面.我应该忘记HttpException而是自己设置所有HTTP代码吗?我该如何解决?
我的Web.Config的自定义错误部分是这样的:
<customErrors mode="On" defaultRedirect="GenericErrorPage.html">
<error statusCode="403" redirect="Forbidden.html" />
</customErrors>
Run Code Online (Sandbox Code Playgroud)
我没有获得Forbidden.html,而是获得了GenericErrorPage.html
对于网络连接,我使用dio和从中检查连接状态Connectivity。
在这里,我检查网络状态:
@override
Widget build(BuildContext context) {
bloc.checkConnectivity(Connectivity());
return StreamBuilder(
stream: bloc.getInitApp,
builder: (context, AsyncSnapshot<InitApp> initApp) {
if (initApp.hasData) {
return prepareMain(initApp.data);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
});
}
Run Code Online (Sandbox Code Playgroud)
checkConnectivity 方法:
checkConnectivity(Connectivity _connectivity) {
_connectivity.onConnectivityChanged.listen((ConnectivityResult result){
if (result == ConnectivityResult.mobile ||
result == ConnectivityResult.wifi) {
fetchInitApp();
}
});
}
Run Code Online (Sandbox Code Playgroud)
但最终,App仅在Android设备上运行,但在iOS(Device / Simulator)上出现此错误:
Error connecting to the service protocol: HttpException: , uri = http://127.0.0.1:1024/ws
Run Code Online (Sandbox Code Playgroud)
我试过Api不带电话Connectivity,应用程序运行良好,但它只能在Android中获取数据,而对于iOS,仍然存在问题。
也是发生在Dart httppackage上。 …
httpexception ×10
asp.net ×6
c# ×6
.net ×2
iis-7 ×2
android ×1
asp.net-core ×1
dart ×1
flutter ×1
http ×1
http-headers ×1
ios ×1
response ×1