C# 错误';客户端发现响应内容类型为';text/html';。。被解释

C# 错误';客户端发现响应内容类型为';text/html';。。被解释,c#,web-services,soap,C#,Web Services,Soap,我正在使用C#并通过自动生成的C#代理对象连接到Web服务。我调用的方法可能会长期运行,有时会超时。我得到不同的错误,有时我得到一个System.Net.WebException或System.Web.Services.Protocols.SoapException。这些异常具有我可以查询的属性,以查找特定类型的错误,从中我可以向用户显示人性化版本的 <httpRuntime requestValidationMode="2.0" /> 但有时我只会收到一个invalidoope

我正在使用C#并通过自动生成的C#代理对象连接到Web服务。我调用的方法可能会长期运行,有时会超时。我得到不同的错误,有时我得到一个
System.Net.WebException
System.Web.Services.Protocols.SoapException
。这些异常具有我可以查询的属性,以查找特定类型的错误,从中我可以向用户显示人性化版本的

 <httpRuntime requestValidationMode="2.0" />
但有时我只会收到一个
invalidooperationexception
,它有以下消息。是否有任何方法可以解释这是什么,而不必通过字符串挖掘我所识别的东西,这些东西感觉非常脏,并且不是国际化不可知的,错误消息可能会以另一种语言返回

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.
-->
--.
 <httpRuntime requestValidationMode="2.0" />

版本信息:Microsoft.NET Framework版本:2.0.50727.312;ASP.NET版本:2.0.50727.833 --. 编辑:
我在web服务器上尝试了这个方法。我已经调试了它,web服务器方法返回(大约一分钟后),没有任何异常。我还在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块。Put和catch(System.Web.Services.protocolo.SoapException)


此外,如果您使用的是.NET 4.0版,则可以为您的webservice对象设置超时。

。默认情况下,所有页面的ValidateRequest都处于打开状态。在以前的版本1.1和2.0中,它仅用于aspx页面。您可以关闭默认验证。在这种情况下,您必须进行尽职调查,确保数据是干净的。使用HtmlEncode。执行以下操作以关闭验证

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

 <pages validateRequest="false" />

 <httpRuntime requestValidationMode="2.0" />


 <httpRuntime requestValidationMode="2.0" />
你可以阅读更多关于这方面的内容 也


希望这能有所帮助。

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

删除web.config文件并再次插入

更改web服务返回类型和后,我出现了此错误

最初是:

[SoapDocumentMethod(OneWay = true)]
[WebMethod]
public void Foo()
{
    return;
}
我决定这样做:

using (wsWebService yourService = new wsWebService())
{
    yourService.Url = "https://myUrlService.com/wsWebService.asmx?op=someOption";
    yourService.UseDefaultCredentials = true; // this line depends on your authentication type
    yourService.SoapVersion = SoapProtocolVersion.Soap11; // asign the version of SOAP
    var result = yourService.SomeMethod("Parameter");
}
在这种情况下,更新web引用会有所帮助

要更新web服务引用,请执行以下操作:

  • 展开解决方案资源管理器
  • 查找Web引用-仅当您在项目中添加了Web服务引用时,此选项才可见
  • 单击鼠标右键,然后单击“更新web引用”

    • 我遇到的问题与SOAP版本有关。
      asmx
      服务被配置为同时接受1.1和1.2两个版本,因此,我认为当您使用该服务时,客户端或服务器不知道解析哪个版本

       <httpRuntime requestValidationMode="2.0" />
      
      要解决此问题,需要添加:


      其中,
      wsWebService
      是作为引用生成的类的名称。

      我编写了webservice。我正在捕获服务器上的所有异常,但没有得到任何异常。在服务器端的方法返回之前,我在客户端收到了这个超时消息。@VinkoVrsalovic:谢谢。又关门了!不过现在我忍不住笑了谢谢你的回答。我猜中间一定有什么东西我不明白。你确定你在捕捉HTTP异常吗?@古斯塔沃:StAdvOpad刚满一年!你刚才回答的问题差不多有那么老了。此外,如果你阅读了整个问题和所有答案,你会发现没有办法在客户机上捕捉到这一点,因为它永远不会到达客户机。我得投你一票否决票,因为你的回答毫无帮助。我希望这不会阻止您尝试帮助。找不到URL。有其他选择吗?URL再次可用
       <httpRuntime requestValidationMode="2.0" />