繁体   English   中英

我无法从HttpError中读取responseStream

[英]I can't read a responseStream from a HttpError

我正在使用.Net Framework 4.0和C#开发ASP.NET MVC 4 Web Api。

当我尝试从HttpError获取响应流时遇到问题。

在这里,我生成HttpError

m_ExceptionLoggerHelper.LogException("ProductionOrderManagementController", "DoBatchPreparation", Singleton.AutomationServiceVM.COGErrors);

HttpError myCustomError = new HttpError(Singleton.AutomationServiceVM.COGErrors) { { "CustomErrorCode", 4 } };
response = Request.CreateErrorResponse(HttpStatusCode.Conflict, myCustomError);

有了Fiddler,我得到了:

HTTP/1.1 409 Conflict
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcRnVlbnRlc1xBdXRvbWF0aW9uTWlkZGxld2FyZVxzcmNcQXV0b21hdGlvbk1pZGRsZXdhcmUuV2ViLkFwaVxhcGlccHJvZHVjdGlvbk9yZGVyc01hbmFnZW1lbnRcTUNHXzIwMTQwODI4MDgyOTQ4XGRvQmF0Y2hQcmVwYXJhdGlvbg==?=
Persistent-Auth: true
X-Powered-By: ASP.NET
Date: Thu, 28 Aug 2014 06:32:36 GMT
Content-Length: 70

{"Message":"Preparation Failed SessionLocked\r\n","CustomErrorCode":4}

在这里,我捕获到异常:

catch (WebException exception)
{
    Stream responseStream = exception.Response.GetResponseStream();
    using (var reader = new StreamReader(responseStream))
    {
        string result = reader.ReadToEnd();
        Console.WriteLine("Exception: " + result);
    }
}

我在这里遇到问题, new StreamReader(responseStream) ,因为我收到了ArgumentException 调试时,我检查responseStream并看到CanRead属性为false。

并且'responseStream.Length' throw an excepción type 'System.ObjectDisposedException'

我究竟做错了什么?

您看到此行为的原因是,响应流已在其他地方使用。

如果要多次读取流,请首先将其内容复制到MemoryStream 然后,您可以根据需要MemoryStream读取MemoryStream

暂无
暂无

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

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