Web 请求失败,ASP.NET 应用程序中出现"414 Request URI too long"



我们有一个ASP.NET应用程序,它在将报表的参数作为WebRequest传递后,请求HTML格式的SSRS 2005报表。只有当请求具有大量多选参数的报告时,应用程序才会失败,从而在webRequest.GetResponse()行引发"414:请求URI过长"错误。

用于提出请求的代码是:

HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
string webRequestURL = _ReportManager.GetRSUrl(reportID); //this passes the report link on the SSRS server
//make the request
Byte[] bytes = Encoding.UTF8.GetBytes("xml_doc=" + HttpUtility.UrlEncode(webRequestURL));
webRequest = (HttpWebRequest)WebRequest.Create(webRequestURL);
webRequest.Method = "POST";
webRequest.ContentLength = bytes.Length;
webRequest.Timeout = Configuration.WebRequestTimeOut;
RSExecution2005.ReportExecutionService rsE = new RSExecution2005.ReportExecutionService();
rsE.Url = Configuration.ReportExecutionServiceUrl2005;
rsE.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.Credentials = rsE.Credentials;
Stream reqStream = null;
reqStream = webRequest.GetRequestStream();
reqStream.Write(bytes, 0, bytes.Length);
reqStream.Close();
webResponse = (HttpWebResponse)webRequest.GetResponse();

由于报告在服务器端失败,我已经查看了IIS和ReportServer属性,以增加字节数的maxUrl、maxRequestLength、MaxQueryString等(根据本文),但应用程序仍然会抛出错误。我已经在web.config文件中直接在IIS管理器上尝试过了。

报表服务器版本为2005,托管在运行IIS 7的Windows server 2008上。


根据David Lively的建议,我尝试通过在正文中放入参数来请求URI。这适用于较小的请求,但对于较大的多选参数仍然失败。修改后的代码如下:

HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
string webRequestURL = _ReportManager.GetRSUrl(reportID); //this passes the report link on the SSRS server
string postData = string.Empty;
string URIrequest = string.Empty;
URIrequest = webRequestURL.Substring(0, webRequestURL.IndexOf("&"));
int requestLen = webRequestURL.Length;
int postDataStart = webRequestURL.IndexOf("&") + 1;
postData = webRequestURL.Substring(postDataStart, (requestLen - postDataStart));
Byte[] bytes1 = Encoding.UTF8.GetBytes(postData);
webRequest = (HttpWebRequest)WebRequest.Create(URIrequest);
webRequest.Method = "POST";
webRequest.ContentType = "application/x-www-form-urlencoded";
webRequest.ContentLength = bytes1.Length;
webRequest.Timeout = Configuration.WebRequestTimeOut;
RSExecution2005.ReportExecutionService rsE = new RSExecution2005.ReportExecutionService();
rsE.Url = Configuration.ReportExecutionServiceUrl2005;
rsE.Credentials = System.Net.CredentialCache.DefaultCredentials;
webRequest.Credentials = rsE.Credentials;
Stream reqStream = webRequest.GetRequestStream();
reqStream.Write(bytes1, 0, bytes1.Length);
reqStream.Close();
webResponse = (HttpWebResponse)webRequest.GetResponse();

即使webRequest的requestURI不存储参数,GetReponse()函数似乎会将参数添加到webRequest的"address"属性中。这会是问题所在吗?如果是的话,该如何修复。

是否可以使用POST变量而不是GET?这样,我知道没有限制,因为您的所有数据都将以数据包而不是HTTP标头的形式发送。

实际上,您可能正在使用代码中的POST。你能查看服务器日志来验证导致失败的URI吗?如果您正在发送POST数据,那么请求uri不应该是问题,除非它与您正在发送的数据无关。

检查服务的绑定设置。我想该服务将允许字符串长度达到8192。将te readerQuotas设置为更大的大小。可能会有所帮助。

<basicHttpBinding>
<binding name="largeBuffer">
<readerQuotas
maxDepth="2147483647"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
<security mode="None"></security></binding>
</basicHttpBinding>

由于您已经在使用POST来获取报告,因此我建议您将当前传递的参数放入请求正文中的查询字符串中。Querystring参数适用于数量有限的参数,但不适用于大量的项。

你能显示webRequestURL的值吗?

它会"太大"。

如果您将参数传递到此URL,它们是否可以在POST正文中?

webRequestURL.IndexOf("&")…这意味着是"?"而不是"&"吗?我猜你构建了一个有效的URL来查询页面,然后通过查找第一个"&"之前的URL将其反向工程为POST请求。。。

但是,GetResponse可能会将正文附加到URL,因为它在URL中看到问号,并假设参数必须在URL中?请尝试使用零参数和无"?"进行更精确的URL匹配。

我在IIS7站点上工作时得到了这个。用注册表破解修复了它,我可以搜索它,但在3/1之前无法工作。同时,如果你在使用ip地址而不是普通URL时出现错误,请尝试,如果你不使用,很可能也是同样的问题。

也有类似的问题,只是POST正在工作,但具有完全相同参数的第二次POST返回414。

设置要求。KeepAlive=false;解决了问题,天知道为什么。

最新更新