加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 04:06:00 所属栏目:百科 来源:网络整理
导读:我们有一个ASP.NET应用程序,将报告的参数作为WebRequest传递后,以 HTML格式请求SSRS 2005报告.当请求具有大量多选参数的报告时,该应用程序将失败,并在webRequest.GetResponse()行中引发“414:请求URI太长”错误. 用于提出请求的代码是: HttpWebRequest web
我们有一个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不应该是一个问题,除非它与您发布的数据无关.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读