如何解释错误'客户端发现'text/html'的响应内容类型

Sco*_*ham 26 c# soap web-services

我正在使用C#并通过自动生成的C#代理对象连接到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.
-->
--.
Run Code Online (Sandbox Code Playgroud)

编辑:我在Web服务器上有一个try-catch方法.我调试了它,web-server方法返回(大约一分钟后),没有任何异常.我还在Web服务中添加了一个未处理的异常处理程序,并且没有命中断点.一旦web服务返回,我就会在客户端中收到此错误,而不是我期望的结果.

Sei*_*bar 42

发生这种情况是因为Web服务中存在未处理的异常,并且.NET运行时正在吐出死亡服务器错误/异常转储页面的HTML黄色屏幕,而不是XML.

由于Web服务的使用者期望使用text/xml标头而不是text/html,因此会抛出该错误.

您应该解决超时的原因(可能是一个冗长的SQL查询?).

另外,请查看Jeff Atwood 博客上的这篇博客文章,该博客解释了如何实现全局未处理的异常处理程序并使用SOAP异常.

  • 找不到网址.还有其他选择 (7认同)

Vin*_*vic 9

这意味着您的消费者期望来自Web服务的XML,但Web服务(如您的错误所示)返回HTML,因为它因超时而失败.

因此,您需要与远程Web服务提供商交谈,让他们知道它失败并采取纠正措施.除非你是webservice的提供者,否则你应该捕获异常并返回XML告诉消费者发生了哪些错误("远程提供者"也应该这样做).


bas*_*gio 5

网络服务器返回 http 500 错误代码。这些错误通常发生在网络服务器上抛出异常并且没有逻辑来捕获它时,它会抛出 http 500 错误。通常可以通过在代码中放置 try-catch 块来解决问题。


小智 5

如果您使用的是.NET 4.0版.默认情况下,对所有页面启用validateRequestion.在以前的版本1.1和2.0中,它仅适用于aspx页面.您可以关闭默认验证.在这种情况下,您必须进行尽职调查,并确保数据是干净的.使用HtmlEncode.执行以下操作以关闭验证

在web.config中为system.web添加以下行

 <httpRuntime requestValidationMode="2.0" />
Run Code Online (Sandbox Code Playgroud)

 <pages validateRequest="false" />
Run Code Online (Sandbox Code Playgroud)

你可以阅读更多关于这个http://www.asp.net/learn/whitepapers/aspnet4/breaking-changeshttp://msdn.microsoft.com/en-us/library/ff649310.aspx

希望这可以帮助.


oek*_*rem 5

由于 web.config 中的配置错误,我发生了这种情况。检查连接字符串等可能是超时的答案。