我正在用ASP.NET Framework4.0和C#开发一个.Net MVC 4 web。
当我试图从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'。
我做错了什么?
发布于 2014-08-28 07:02:14
您之所以看到这种行为,是因为响应流已经被其他地方消耗掉了。
如果要多次读取流,请先将其内容复制到MemoryStream。然后,您可以随心所欲地阅读MemoryStream。
https://stackoverflow.com/questions/25542391
复制相似问题