简体   繁体   English

WebRequest在ASP.NET应用程序中失败,“414 Request URI太长”

[英]WebRequest fails with “414 Request URI too long” in ASP.NET application

We have an ASP.NET application that requests an SSRS 2005 report in HTML format after passing the parameters for the report as a WebRequest. 我们有一个ASP.NET应用程序,它在将报告的参数作为WebRequest传递后,以HTML格式请求SSRS 2005报告。 The application only fails when a report with a large number of multi-select parameters is requested, throwing a "414: Request URI too long" error at the webRequest.GetResponse() line. 只有在请求具有大量多选参数的报表时,应用程序才会失败,并在webRequest.GetResponse()行中抛出“414:Request URI too long”错误。

The code used to make the request is: 用于发出请求的代码是:

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();

As the report fails on the server side, I have looked into IIS and ReportServer properties to increase the maxUrl, maxRequestLength, MaxQueryString, etc. in terms of bytes (as per this article ) but the application still throws an error. 由于报告在服务器端失败,我已经查看了IIS和ReportServer属性,以字节方式增加maxUrl,maxRequestLength,MaxQueryString等(根据本文 ),但应用程序仍然会抛出错误。 I have tried this in the web.config files and directly on the IIS manager. 我已经在web.config文件中尝试过这个并直接在IIS管理器上。

The reporting server version in 2005 and it is hosted on Windows Server 2008, which is running IIS 7. 2005年的报告服务器版本,它托管在运行IIS 7的Windows Server 2008上。


On David Lively's advise I tried requesting the URI by putting the parameters in the body. 在David Lively的建议中,我尝试通过将参数放在正文中来请求URI。 This works for smaller requests, but still fails for large multi-select parameters. 这适用于较小的请求,但仍然无法用于大型多选参数。 The amended code is as follows: 修订后的代码如下:

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();

Even though the requestURI of the webRequest does not store parameters, it seems that the GetReponse() function adds the parameters to the 'address' property of the webRequest. 即使webRequest的requestURI不存储参数,似乎GetReponse()函数也将参数添加到webRequest的'address'属性中。 could this be the problem? 这可能是问题吗? if so, how can it be fixed. 如果是的话,怎么解决呢。

Is it possible for you to use POST variables instead of GET? 您是否可以使用POST变量而不是GET? That way, there are no limits that I'm aware of, as all of your data will be sent in packets instead of HTTP headers. 这样,我没有任何限制,因为所有数据都将以数据包而不是HTTP标头发送。

Actually it looks like you might be using POST from what's in your code. 实际上,您可能正在使用代码中的POST。 Can you look in the server logs to verify the URI that is causing this to fail? 您是否可以查看服务器日志以验证导致此失败的URI? If you're sending POST data, the request uri shouldn't be an issue unless it's unrelated to the data you're POSTing. 如果您要发送POST数据,请求uri不应成为问题,除非它与您正在发布的数据无关。

Check your service's binding settings. 检查服务的绑定设置。 I guess the service will allow the string upto 8192 length. 我想该服务将允许字符串长达8192。 Set te readerQuotas to a larger size. 将te readerQuotas设置为更大的尺寸。 Might help. 可能有帮助。

... ...

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

..... .....

Since you're already using POST to fetch the report, I'd suggest putting the parameters that you're currently passing in the query string in the request body, instead. 由于您已经使用POST来获取报告,因此我建议您将当前在查询字符串中传递的参数放在请求正文中。 Querystring parameters work fine for a limited number of parameters, but aren't appropriate for a large number of items. 查询字符串参数适用于有限数量的参数,但不适用于大量项目。

Can you show the value of webRequestURL? 你能展示webRequestURL的价值吗?

It's going to be "too big". 这将是“太大”。

If you are passing parameters to this URL, can they be in the POST body instead? 如果要将参数传递给此URL,它们是否可以在POST主体中?

webRequestURL.IndexOf("&") ... Is this meant to be "?" webRequestURL.IndexOf(“&”)......这是否意味着“?” instead of "&"? 代替 ”&”? I'm guessing you construct a valid URL for querying the page and then reverse engineer it to be a POST request by looking for the URL before the first '&'... 我猜你构建了一个有效的URL来查询页面,然后通过在第一个'&'之前查找URL来将其反向工程为POST请求...

However, it's possible the GetResponse is appending the body to the URL because it sees the Question Mark in the URL and assumes that the parameters must go in the URL? 但是,GetResponse可能会将正文附加到URL,因为它会在URL中看到问号并假设参数必须包含在URL中? Try doing a more exact URL match with zero parameters and no '?'. 尝试使用零参数进行更精确的URL匹配,而不是'?'。

I got this at work on my IIS7 site. 我在IIS7网站上运行了这个。 Got it fixed with a registry hack, i can search it up but won't work before 3/1. 通过注册表黑客修复它,我可以搜索它但在3/1之前无法工作。 Meanwhile, try if you get the error when you use the ip-address in stead of the normal URL, when you don't, chances are high it is the same problem. 同时,如果你在使用ip-address而不是正常的URL时得到错误,请尝试,否则,可能性很高,这是同样的问题。

Had a similar issue, except that POST was working, but second POST with exactly same parameters returned 414. 有一个类似的问题,除了POST工作,但第二个POST与完全相同的参数返回414。

Setting req.KeepAlive = false; 设置req.KeepAlive = false; solved the problem, God knows why. 解决了问题,上帝知道为什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM