Asp.net错误“System.Web.HttpException: 超过了最大请求长度”解决办法
2014-03-05 21:33
846 查看
方法一:
在通常(不局限于此)在上传文件大于ASP.NET默认的4M 或 Web.config 的的配置时会抛出此异常;
在通过WebService处理大数据量数据时出现如下错误:System.Web.Services.Protocols.SoapException: 在运行配置文件中指定的扩展时出现异常。 ---> System.Web.HttpException: 超过了最大请求长度。
at System.Web.HttpRequest.GetEntireRawContent()
at System.Web.HttpRequest.get_InputStream()
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
--- 内部异常堆栈跟踪的结尾 ---
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)解决方法:是因为传入的参数大于系统默认配置的值,asp.net web service默认的请求长度是4M。修改配置可以在web.config中重新设置,如下:
Web.config 配置:
复制代码
把上面的maxRequestLength="4096" 改为数值大一点的即可解决 例如:maxRequestLength="40960"
如果光是调整Web.config的 maxRequestLength 是治标不治本的。今天研究了下在网上找到一个方法,代码大致如下:Global.asax.cs需要多引入命名空间
Using System.Web.Configuration;
void Application_BeginRequest(object sender, EventArgs e)
{
//本代码的功能是检查页面请求的大小,如果超过了配置文件maxRequestLength的设定值,就提示用户超过了所允许的文件大小。
//从配置文件里得到配置的允许上传的文件大小
HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection("system.web/httpRuntime");
//maxRequestLength 为整个页面的大小,不仅仅是上传文件的大小,所以扣除 100KB 的大小,
//maxRequestLength单位为KB
int maxRequestLength = (runTime.MaxRequestLength) * 1024;
//当前请求上下文的HttpApplication实例
//HttpContext context = ((HttpApplication)sender).Context;
//判断请求的内容长度是否超过了设置的字节数
if (Request.ContentLength > maxRequestLength)
{
#region 不理解这些代码存在的意义
/*
//得到服务对象
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
//检查请求是否包含正文数据
if (workerRequest.HasEntityBody())
{
//请求正文数据的长度
int requestLength = workerRequest.GetTotalEntityBodyLength();
//得到加载的初始字节数
int initialBytes = 0;
if (workerRequest.GetPreloadedEntityBody() != null)
initialBytes = workerRequest.GetPreloadedEntityBody().Length;
//检查是否所有请求数据可用
if (!workerRequest.IsEntireEntityBodyIsPreloaded())
{
byte[] buffer = new byte[512000];
//设置要接收的字节数为初始字节数
int receivedBytes = initialBytes;
//读取数据,并把所有读取的字节数加起来,判断总的大小
while (requestLength - receivedBytes >= initialBytes)
{
//读取下一块字节
initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
//更新接收到的字节数
receivedBytes += initialBytes;
}
initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
}
}
*/
#endregion
//注意这里可以跳转,可以直接终止;在VS里调试时候得不到想要的结果,通过IIS才能得到想要的结果;FW4.0经典或集成都没问题
Response.Write("请求大小" + Request.ContentLength);
Response.End();
}
}
复制代码
方法二:也可以直接修改.net环境的默认配置,这样免得一个个项目去配置了,一劳永逸。
找到:C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG\machine.config文件(版本及系统不同,目录稍有差异),找到“maxRequestLength="4096"”这行,将其中的4096换成较大的数值,这里的单位是KB,这样就可以了。
方法三:
复制代码
在通常(不局限于此)在上传文件大于ASP.NET默认的4M 或 Web.config 的的配置时会抛出此异常;
在通过WebService处理大数据量数据时出现如下错误:System.Web.Services.Protocols.SoapException: 在运行配置文件中指定的扩展时出现异常。 ---> System.Web.HttpException: 超过了最大请求长度。
at System.Web.HttpRequest.GetEntireRawContent()
at System.Web.HttpRequest.get_InputStream()
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
--- 内部异常堆栈跟踪的结尾 ---
at System.Web.Services.Protocols.SoapServerProtocol.Initialize()
at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing)解决方法:是因为传入的参数大于系统默认配置的值,asp.net web service默认的请求长度是4M。修改配置可以在web.config中重新设置,如下:
Web.config 配置:
复制代码
把上面的maxRequestLength="4096" 改为数值大一点的即可解决 例如:maxRequestLength="40960"
如果光是调整Web.config的 maxRequestLength 是治标不治本的。今天研究了下在网上找到一个方法,代码大致如下:Global.asax.cs需要多引入命名空间
Using System.Web.Configuration;
void Application_BeginRequest(object sender, EventArgs e)
{
//本代码的功能是检查页面请求的大小,如果超过了配置文件maxRequestLength的设定值,就提示用户超过了所允许的文件大小。
//从配置文件里得到配置的允许上传的文件大小
HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection("system.web/httpRuntime");
//maxRequestLength 为整个页面的大小,不仅仅是上传文件的大小,所以扣除 100KB 的大小,
//maxRequestLength单位为KB
int maxRequestLength = (runTime.MaxRequestLength) * 1024;
//当前请求上下文的HttpApplication实例
//HttpContext context = ((HttpApplication)sender).Context;
//判断请求的内容长度是否超过了设置的字节数
if (Request.ContentLength > maxRequestLength)
{
#region 不理解这些代码存在的意义
/*
//得到服务对象
IServiceProvider provider = (IServiceProvider)context;
HttpWorkerRequest workerRequest = (HttpWorkerRequest)provider.GetService(typeof(HttpWorkerRequest));
//检查请求是否包含正文数据
if (workerRequest.HasEntityBody())
{
//请求正文数据的长度
int requestLength = workerRequest.GetTotalEntityBodyLength();
//得到加载的初始字节数
int initialBytes = 0;
if (workerRequest.GetPreloadedEntityBody() != null)
initialBytes = workerRequest.GetPreloadedEntityBody().Length;
//检查是否所有请求数据可用
if (!workerRequest.IsEntireEntityBodyIsPreloaded())
{
byte[] buffer = new byte[512000];
//设置要接收的字节数为初始字节数
int receivedBytes = initialBytes;
//读取数据,并把所有读取的字节数加起来,判断总的大小
while (requestLength - receivedBytes >= initialBytes)
{
//读取下一块字节
initialBytes = workerRequest.ReadEntityBody(buffer, buffer.Length);
//更新接收到的字节数
receivedBytes += initialBytes;
}
initialBytes = workerRequest.ReadEntityBody(buffer, requestLength - receivedBytes);
}
}
*/
#endregion
//注意这里可以跳转,可以直接终止;在VS里调试时候得不到想要的结果,通过IIS才能得到想要的结果;FW4.0经典或集成都没问题
Response.Write("请求大小" + Request.ContentLength);
Response.End();
}
}
复制代码
方法二:也可以直接修改.net环境的默认配置,这样免得一个个项目去配置了,一劳永逸。
找到:C:\WINDOWS\Microsoft.NET\Framework\v1.1.4322\CONFIG\machine.config文件(版本及系统不同,目录稍有差异),找到“maxRequestLength="4096"”这行,将其中的4096换成较大的数值,这里的单位是KB,这样就可以了。
方法三:
复制代码
相关文章推荐
- Asp.net错误“System.Web.HttpException: 超过了最大请求长度”解决办法
- asp.net MVC 上传文件 System.Web.HttpException: 超过了最大请求长度
- System.Web.HttpException: 超过了最大请求长度!解决办法[转帖]
- 关于"System.Web.HttpException: 超过了最大请求长度。"错误的解决
- System.Web.HttpException: 超过了最大请求长度!解决办法
- System.Web.HttpException: 超过了最大请求长度!解决办法[转帖]
- asp.net MVC 上传文件 System.Web.HttpException- 超过了最大请求长度
- System.Web.HttpException: 超过了最大请求长度!解决办法
- System.Web.HttpException: 超过了最大请求长度!解决办法
- 关于"System.Web.HttpException: 超过了最大请求长度。"错误的解决
- 解决在运行配置文件中指定的扩展时出现异常。 ---> System.Web.HttpException: 超过了最大请求长度问
- C# Webservice 解决在运行配置文件中指定的扩展时出现异常。 ---> System.Web.HttpException: 超过了最大请求长度问
- 处理“System.Web.HttpException: 超过了最大请求长度”
- 在运行配置文件中指定的扩展时出现异常。 System.Web.HttpException: 超过了最大请求长度。
- orchard上传文件提示System.Web.HttpException: 超过了最大请求长度
- asp.net Chartr控件异常: System.Web.HttpException: 为 ChartImg.axd 执行子请求时出错(已解决)
- System.Web.HttpException: 超过了最大请求长度。
- 如何避免System.Web.HttpException: 超过了最大请求长度。
- [文件上传]System.Web.HttpException: 超过了最大请求长度
- 解决Asp.net Web网站发布到服务器上运行时,某界面传参数过多,造成的“此请求的查询字符串的长度超过配置的 maxQueryStringLength 值”错误。