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

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

但有时我只是得到一个InvalidOperationException ,它有以下消息。 有没有什么方法可以解释这是什么,如果没有通过字符串挖掘我认识的东西,感觉非常脏,并且不是国际化不可知,错误信息可能会以不同的语言回来。

 Client found response content type of 'text/html; charset=utf-8', but expected 'text/xml'. The request failed with the error message: --   Request timed out.  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; }    

Server Error in '/PerformanceManager' Application.

Request timed out.

Description: 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.

Exception Details: System.Web.HttpException: Request timed out.

Source Error:

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.

Stack Trace:

 [HttpException (0x80004005): Request timed out.] 


Version Information: Microsoft .NET Framework Version:2.0.50727.312; ASP.NET Version:2.0.50727.833
--.

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

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

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

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

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

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

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

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

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

   

   

您可以在http://www.asp.net/learn/whitepapers/aspnet4/breaking-changes上阅读更多相关内容http://msdn.microsoft.com/en-us/library/ff649310.aspx

希望这可以帮助。

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

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

删除web.config文件并再次插入。 http://forums.asp.net/post/916808.aspx

您的Web服务是否在IIS中正确配置? 它使用的池,ASP.NET(2.0)的版本设置? 你能浏览.asmx吗?

谈到exception,尝试在访问您的Web服务的行中放置一个try-catch块。 Put和catch(System.Web.Services.Protocolos.SoapException)。

此外,您可以为Web服务对象设置超时。

更改Web服务返回类型和SoapDocumentMethod后,我遇到此错误。

最初它是:

 [WebMethod] public int Foo() { return 0; } 

我决定点火,忘记这样的类型 :

 [SoapDocumentMethod(OneWay = true)] [WebMethod] public void Foo() { return; } 

在这种情况下,更新Web引用有帮助。

要更新Web服务引用:

  • 展开解决方案浏览器
  • 找到Web引用 – 只有在项目中添加了Web服务引用时,才会显示此引用
  • 右键单击并单击“更新Web引用”

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

要解决这个问题,必须添加:

 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"); } 

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