gpt4 book ai didi

c# - 如何解析来自 WebException 响应的内部异常

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:17 25 4
gpt4 key购买 nike

我有 C# 代码对 OData Web 服务执行 POST,从而将记录插入到数据库中。如果出现异常,比如违反主键,我想捕获并记录错误信息。但是,该消息包含在 XML 中。这是我的异常处理程序:

catch (WebException ex)
{
if (ex.Status == WebExceptionStatus.ProtocolError)
{
string responseText = string.Empty;

using (Stream responseStream = ((HttpWebResponse)ex.Response).GetResponseStream())
{
using (StreamReader streamReader = new StreamReader(responseStream))
{
responseText = streamReader.ReadToEnd();
}
}
Debug.WriteLine(responseText);
return responseText;
}
else
{
Debug.WriteLine(ex.Message.ToString());
return ex.Message.ToString();
}
}

这是我在 responseText 中得到的内容:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<error xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<code></code>
<message xml:lang="en-US">An error occurred while processing this request.</message>
<innererror>
<message>An error occurred while updating the entries. See the inner exception for details.</message>
<type>System.Data.UpdateException</type>
<stacktrace> at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)&#xD;
at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)&#xD;
at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)&#xD;
at System.Data.Services.Providers.ObjectContextServiceProvider.SaveChanges()&#xD;
at System.Data.Services.DataService 1.HandleNonBatchRequest(RequestDescription description)&#xD;
at System.Data.Services.DataService 1.HandleRequest()</stacktrace>
<internalexception>
<message>Violation of PRIMARY KEY constraint 'PK_Customer_CustomerId'. Cannot insert duplicate key in object 'VertexBilling.Customer'.&#xD;
The statement has been terminated.</message>
<type>System.Data.SqlClient.SqlException</type>
<stacktrace> at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)&#xD;
at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)&#xD;
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()&#xD;
at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)&#xD;
at System.Data.SqlClient.SqlDataReader.ConsumeMetaData()&#xD;
at System.Data.SqlClient.SqlDataReader.get_MetaData()&#xD;
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)&#xD;
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async)&#xD;
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, DbAsyncResult result)&#xD;
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)&#xD;
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)&#xD;
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)&#xD;
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)&#xD;
at System.Data.Mapping.Update.Internal.DynamicUpdateCommand.Execute(UpdateTranslator translator, EntityConnection connection, Dictionary 2 identifierValues, List 1 generatedValues)&#xD;
at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)</stacktrace>
</internalexception>
</innererror>
</error>

我需要从 innererror 中获取来自 internalexception 的消息。最简单的方法是什么?

最佳答案

您应该使用 XML 工具包,您已经在 .net 中使用了它,并且它消除了将 xml 视为文本时的歧义问题(这是一场等待发生的噩梦,在上面的 xml 中,您有两个消息节点)

  1. 将字符串加载到 XmlDocument
  2. 使用//error/innerError/internalexception/message xpath提取需要的节点

或者,您可以构建一小段代码,将 innerException 提取/反序列化为 Exception 实例,并可以将其重新用于其他调用。

如果你不知道怎么做,howler,我会搞砸的。

关于c# - 如何解析来自 WebException 响应的内部异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4271685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com