如何解释"客户端找到响应内容类型为" text / html"的错误。
我正在使用Cand通过自动生成的Cproxy对象连接到WebService。我正在调用的方法可能会长时间运行,有时会超时。我返回了不同的错误,有时会收到" System.Net.WebException"或者" System.Web.Services.Protocols.SoapException"。这些异常具有一些属性,我可以查询这些属性以查找特定的错误类型,从中可以向用户显示人性化的版本。
但是有时候我只是得到一个InvalidOperationException
,它包含以下消息。我有什么办法可以解释这是什么,而不必深入研究我认识的东西的字符串,那感觉很脏,并且与国际化无关,错误消息可能会以另一种语言返回。
Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'. The request failed with the error message: -- <html> <head> <title>Request timed out.</title> <style> body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px} b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px} H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red } H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon } pre {font-family:"Lucida Console";font-size: .9em} .marker {font-weight: bold; color: black;text-decoration: none;} .version {color: gray;} .error {margin-bottom: 10px;} .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; } </style> </head> <body bgcolor="white"> <span><H1>Server Error in '/PerformanceManager' Application.<hr width=100% size=1 color=silver></H1> <h2> <i>Request timed out.</i> </h2></span> <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "> <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br><br> <b> Exception Details: </b>System.Web.HttpException: Request timed out.<br><br> <b>Source Error:</b> <br><br> <table width=100% bgcolor="#ffffcc"> <tr> <td> <code> An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code> </td> </tr> </table> <br> <b>Stack Trace:</b> <br><br> <table width=100% bgcolor="#ffffcc"> <tr> <td> <code><pre> [HttpException (0x80004005): Request timed out.] </pre></code> </td> </tr> </table> <br> <hr width=100% size=1 color=silver> <b>Version Information:</b>?Microsoft .NET Framework Version:2.0.50727.312; ASP.NET Version:2.0.50727.833 </font> </body> </html> <!-- [HttpException]: Request timed out. --> --.
编辑:
我在Web服务器上尝试了该方法。我已经调试了它,并且web-server方法返回(大约一分钟后),没有任何异常。我还在Web服务中添加了一个未处理的异常处理程序,并且没有命中断点。 Web服务返回后,我会在客户端中收到此错误,而不是预期的结果。
解决方案
这意味着使用者希望从Web服务中获取XML,但正如错误所示,Web服务返回HTML,因为由于超时而失败。
因此,我们需要与远程Web服务提供商联系,以使他们知道发生故障并采取纠正措施。除非我们是Web服务的提供者,否则我们应该捕获异常并返回XML告知使用者发生了哪个错误("远程提供者"也应该这样做)。
发生这种情况是因为Web服务中存在未处理的异常,并且.NET运行时吐出了它的HTML黄色屏幕上的死亡服务器错误/异常转储页面,而不是XML。
由于Web服务的使用者期望使用text / xml标头,而获得了text / html,因此抛出该错误。
我们应该解决超时的原因(也许是冗长的SQL查询?)。
另外,请在Jeff Atwood的博客上查阅此博客文章,该文章说明了如何实现全局未处理的异常处理程序以及如何使用SOAP异常。
Web服务器返回http 500错误代码。这些错误通常是在Web服务器上引发异常并且没有逻辑捕获该异常时发生的,因此它会抛出http 500错误。通常,我们可以通过在代码中放置try-catch块来解决问题。
Web服务是否在IIS中正确配置?其使用的池,设置了ASP.NET(2.0)的版本?我们可以浏览.asmx吗?
在谈论异常时,请尝试在访问Web服务的行中放置try-catch块。放置并捕获(System.Web.Services.Protocolos.SoapException)。
另外,我们可以为Web服务对象设置超时。