c# – ASP.NET应用程序中的WebRequest失败与“414请求URI太长”

前端之家收集整理的这篇文章主要介绍了c# – ASP.NET应用程序中的WebRequest失败与“414请求URI太长”前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我们有一个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,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,bytes1.Length);
reqStream.Close();
webResponse = (HttpWebResponse)webRequest.GetResponse();

即使webRequest的requestURI不存储参数,似乎GetReponse()函数将参数添加到webRequest的“address”属性.这可能是问题吗?如果是这样,怎么办呢?

解决方法

您可以使用POST变量代替GET吗?这样,我没有意义的限制,因为所有的数据都将以数据包的形式发送,而不是HTTP头.

其实看起来你可能正在使用你的代码中的POST.您可以查看服务器日志来验证造成此失败的URI?如果您发送POST数据,请求uri不应该是一个问题,除非它与您发布的数据无关.

原文链接:https://www.f2er.com/csharp/92772.html

猜你在找的C#相关文章