Rhi*_*ino 3 c# .net-4.0 httpwebresponse
我们目前在阅读我们过去没有遇到任何问题的ResponseStream时遇到问题.自从昨晚将.NET 4.0 Framwework添加到我们的服务器并分配IIS以使用新框架后,我们在尝试使用以下语句(responseStream = httpResponse.GetResponseStream();)尝试读取responseStream时遇到了一些不同的异常.到目前为止,一切都完美无缺.所以,我正在寻找有关如何从响应中读取的更改/改进.我已经粘贴了我们正在使用的以下代码以及我们遇到的例外情况.
.NET Framework 4.0 Windows Server 2003
HttpWebResponse httpResponse;
Stream responseStream;
//Accept All Certificate Policy
ServicePointManager.ServerCertificateValidationCallback += delegate { return true; };
HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(new Uri(theUrl));
httpRequest.Method = "POST";
httpRequest.KeepAlive = false;
httpRequest.Timeout = timeOut;
try
{
httpResponse = (HttpWebResponse)httpRequest.GetResponse();
responseStream = httpResponse.GetResponseStream();
}
Run Code Online (Sandbox Code Playgroud)
'httpResponse.GetResponseStream().Length'抛出类型'System.NotSupportedException'的异常long {System.NotSupportedException}
'httpResponse.GetResponseStream().Position'抛出类型'System.NotSupportedException'的异常long {System.NotSupportedException}
{"此流不支持搜索操作."} System.SystemException {System.NotSupportedException}
问候,
麦克风
您没有显示尝试使用长度的代码 - 但基本上,您无法保证您将知道流的长度.服务器可能没有给出内容长度标头.
如果你需要弄乱流,最好将所有数据复制到一个MemoryStream.使用.NET 4,这非常简单:
MemoryStream ms = new MemoryStream();
responseStream.CopyTo(ms);
Run Code Online (Sandbox Code Playgroud)
如果你实际上并不需要任何长度(例如你只想将结果作为XML文档加载),那么只需从流中读取而不需要调用Length.
请注意,如果可能的话,在初始化时声明变量通常是一种好习惯 - 您应该使用usingWeb响应和响应流的语句.
| 归档时间: |
|
| 查看次数: |
5711 次 |
| 最近记录: |