【问题标题】:WebRequest fails with "414 Request URI too long" in ASP.NET applicationWebRequest 在 ASP.NET 应用程序中因“414 Request URI too long”而失败
【发布时间】:2012-11-23 13:29:41
【问题描述】:

我们有一个 ASP.NET 应用程序,它在将报表参数作为 WebRequest 传递后请求 HTML 格式的 SSRS 2005 报表。仅当请求具有大量多选参数的报告时应用程序才会失败,在webRequest.GetResponse() 行抛出“414: Request URI too long”错误。

用于发出请求的代码是:

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 等字节数(根据 this article),但应用程序仍然抛出错误。我已经在 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' 属性中。这可能是问题吗?如果是这样,如何解决。

【问题讨论】:

    标签: c# iis reporting-services httpwebrequest getresponse


    【解决方案1】:

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

    【讨论】:

    • 感谢 David 的想法,我尝试将参数放入请求正文中,它适用于较小的参数选择,但在大量多选参数时仍然失败并出现相同的错误已选中。
    • 请在上面的原始问题中找到我修改后的代码和编辑。干杯!
    【解决方案2】:

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

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

    【讨论】:

    • 感谢 Kasapo,我查看了服务器日志,可以看到发出的请求是 POST 请求。但是,我看不到任何提及我收到的 414 错误,这很奇怪。我正在查看 reportServer 实例的 IIS 日志。
    【解决方案3】:

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

    ...

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

    .....

    【讨论】:

      【解决方案4】:

      你能显示 webRequestURL 的值吗?

      它会“太大”。

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

      【讨论】:

        【解决方案5】:

        webRequestURL.IndexOf("&") ...这是否意味着“?”代替 ”&”?我猜你构造了一个有效的 URL 来查询页面,然后通过在第一个 '&' 之前查找 URL 将其反向工程为 POST 请求......

        但是,GetResponse 可能会将正文附加到 URL,因为它在 URL 中看到问号并假定参数必须进入 URL?尝试使用零参数且不使用“?”进行更精确的 URL 匹配。

        【讨论】:

          【解决方案6】:

          我在我的 IIS7 网站上得到了这个。用注册表黑客修复了它,我可以搜索它,但在 3/1 之前无法使用。同时,如果你使用 ip-address 代替普通 URL 时出现错误,请尝试,如果没有,则很有可能是同样的问题。

          【讨论】:

            【解决方案7】:

            有一个类似的问题,除了 POST 工作正常,但具有完全相同参数的第二个 POST 返回 414。

            设置 req.KeepAlive = false;解决了这个问题,天知道为什么。

            【讨论】:

              猜你喜欢
              • 2020-07-18
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-08-27
              • 2016-01-25
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多