System.Net.WebException使用HttpWebResponse运行WCFRestful服务方法时显示错误 [英] System.Net.WebException Error dispalying while running a WCFRestful Service method using HttpWebResponse

查看:75
本文介绍了System.Net.WebException使用HttpWebResponse运行WCFRestful服务方法时显示错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试使用HttpWebRequest

对象从URL获取响应。这是我的代码:

 HttpWebRequest myReq =(HttpWebRequest)WebRequest.Create(url); 
WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
System.Drawing.Image oImg = System.Drawing.Image.FromStream(stream);



这里我的URL长度是2080个字符,我需要发送到5000个字符。这种情况有什么解决方案吗?请告诉我。



当我运行示例控制台应用程序时,我收到以下错误:

遥控器服务器返回错误404在system.net.httpwebrequest.getresponse找不到



URL:

http:// localhost / RestfulService / Service.svc / runPowershell?strQuery =<?xml version ='1.0'?>< wql host = '10 .10.10.10''username ='abcd'password ='12545'>< query id ='0 。'><![CDATA [Get-WmiObject -query'从Win32_OperatingSystem中选择CSName']>< / query>< query id ='1。'><![CDATA [Get-WmiObject] -query'从Win32_OperatingSystem选择标题']]>< / query>< query id ='2。'><![CDATA [Get-WmiObject -query'从Win32_OperatingSystem选择CSDVersion']]>< ; / query>< query id ='3。'><![CDATA [Get-WmiObject -query'从Win32_OperatingSystem选择FreePhysicalMemory']]>< / query>< query id ='4。' >< ![CDATA [Get-WmiObject -query'从Win32_OperatingSystem选择TotalVisibleMemorySize']]>< / query>< query id ='5。'><![CDATA [Get-WmiObject -query'select NumberOfProcesses from Win32_OperatingSystem']]>< / query>< query id ='6。'><![CDATA [Get-WmiObject -query'从Win32_DiskDrive选择大小']]>< / query>< query id ='7。'><![CDATA [Get-WmiObject -query'从Win32_Processor中选择LoadPercentage']]>< / query>< query id ='8.1'><![CDATA [Get-WmiObject -query'从Win32_OperatingSystem选择FreePhysicalMemory']]>< / query>< query id ='8.2'><![CDATA [Get-WmiObject -query'从Win32_OperatingSystem选择TotalVisibleMemorySize']] >< / query>< query id ='9.1'><![CDATA [Get-WmiObject -query'从Win32_LogicalDisk中选择FreeSpace']]>< / query>< query id ='9.2 '><![CDATA [Get-WmiObject -query'从Win32_LogicalDis中选择大小k']]>< / query>< query id = '10 .1'><![CDATA [Get-WmiObject -query'从Win32_LogicalDisk中选择FreeSpace']]>< / query>< query id = '10 .2'><![CDATA [Get-WmiObject -query'从Win32_LogicalDisk选择大小']]>< / query>< query id = '11 .1'><![CDATA [获取] -WmiObject -query'从Win32_LogicalDisk中选择FreeSpace']]>< / query>< query id = '11。''><![CDATA [Get-WmiObject -query'从Win32_LogicalDisk中选择大小']]> < / query>< / wql>



网址长度:2371

解决方案

您有效查询字符串中的字符。

在客户端,您需要在发送请求之前使用

Uri.EscapeUriString(string)

Uri.EscapeDataString(string)

这两种方法都限制在32766个字符以内输入字符串。



限制也可以开启服务方面。

如果您的服务托管在IIS中,我认为默认的最大查询字符串大约是4KB。



对于IIS 7,您可以更改允许的最大值url和查询字符串长度:

appcmd set config / section:requestfiltering /requestlimits.maxurl:<size in bytes >



appcmd set config / section:requestfiltering /requestlimits.maxquerystring:<size in bytes >



您可以找到更多详细信息其他 [ ^ ]。

您还可以使用IIS管理器此处 [ ^ ]

请求筛选是Internet信息服务(IIS)7.0中引入的内置安全功能,它取代了通过IIS 6.0的UrlScan插件提供的大部分功能。请求筛选功能的所有设置都位于<   requestFiltering  > 元素,其中包含以下每个功能区域的多个子元素:



1-转到IIS。
2 - 点击请求过滤
3-右键单击并选择编辑功能设置
4-调整请求限制

调整请求限制是限制设置请求的长度和查询字符串的长度。


Hi,

I am trying to get a response from a URL using the HttpWebRequest
object. This is my code below:

HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
WebResponse myResp = myReq.GetResponse();
Stream stream = myResp.GetResponseStream();
System.Drawing.Image oImg = System.Drawing.Image.FromStream(stream);


here my URL length is 2080 characters and I need to send up to 5000 characters. Is there any solutions for this scenario? Please let me know.

When I'm running the sample console application, I'm getting the following error:
"the remote server returned an error 404 not found at system.net.httpwebrequest.getresponse"

URL:
http://localhost/RestfulService/Service.svc/runPowershell?strQuery=<?xml version='1.0'?><wql host='10.10.10.10'' username='abcd' password='12545'><query id='0.' ><![CDATA[ Get-WmiObject -query 'select CSName from Win32_OperatingSystem']]></query><query id='1.' ><![CDATA[ Get-WmiObject -query 'select Caption from Win32_OperatingSystem ']]></query><query id='2.' ><![CDATA[ Get-WmiObject -query 'select CSDVersion from Win32_OperatingSystem']]></query><query id='3.' ><![CDATA[ Get-WmiObject -query 'select FreePhysicalMemory from Win32_OperatingSystem']]></query><query id='4.' ><![CDATA[ Get-WmiObject -query 'select TotalVisibleMemorySize from Win32_OperatingSystem']]></query><query id='5.' ><![CDATA[ Get-WmiObject -query 'select NumberOfProcesses from Win32_OperatingSystem']]></query><query id='6.' ><![CDATA[ Get-WmiObject -query 'select Size from Win32_DiskDrive']]></query><query id='7.' ><![CDATA[ Get-WmiObject -query 'select LoadPercentage from Win32_Processor']]></query><query id='8.1' ><![CDATA[ Get-WmiObject -query 'select FreePhysicalMemory from Win32_OperatingSystem']]></query><query id='8.2' ><![CDATA[ Get-WmiObject -query 'select TotalVisibleMemorySize from Win32_OperatingSystem']]></query><query id='9.1' ><![CDATA[ Get-WmiObject -query 'select FreeSpace from Win32_LogicalDisk']]></query><query id='9.2' ><![CDATA[ Get-WmiObject -query 'select size from Win32_LogicalDisk']]></query><query id='10.1' ><![CDATA[ Get-WmiObject -query 'select FreeSpace from Win32_LogicalDisk']]></query><query id='10.2' ><![CDATA[ Get-WmiObject -query 'select size from Win32_LogicalDisk']]></query><query id='11.1' ><![CDATA[ Get-WmiObject -query 'select FreeSpace from Win32_LogicalDisk']]></query><query id='11.2' ><![CDATA[ Get-WmiObject -query 'select size from Win32_LogicalDisk']]></query></wql>

URL Lenght: 2371

解决方案

You have invalid characters in your query string.
On client side you need to escape it before you send request by using
Uri.EscapeUriString(string) or
Uri.EscapeDataString(string)
Both these methods are limited to 32766 characters in input strings.

The limitation could also be on service side.
If your service is hosted in IIS, I think default max query string is about 4KB.

For IIS 7 you can change maximum allowed url and query string length:
appcmd set config /section:requestfiltering /requestlimits.maxurl:<size in bytes>
and
appcmd set config /section:requestfiltering /requestlimits.maxquerystring:<size in bytes>

You can find more details here[^].
You can also use IIS Manager as described here[^]


Request Filtering is a built-in security feature that was introduced in Internet Information Services (IIS) 7.0, and replaces much of the functionality that was available through the UrlScan add-on for IIS 6.0. All of the settings for the request filtering feature are located within the <requestFiltering> element, which contains several child elements for each of the following feature areas:



1- Go to IIS.
2-Click on "Request Filtering"
3- Right Click and select Edit Feature Settings
4- Adjust Request Limits

Adjust Request Limits is where the limits to length of the request and the lenth of the query string are set.


这篇关于System.Net.WebException使用HttpWebResponse运行WCFRestful服务方法时显示错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆