C# 如何解释错误“客户端发现响应内容类型为 'text/html'..
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/115319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can the error 'Client found response content type of 'text/html'.. be interpreted
提问by Scott Langham
I'm using C# and connecting to a WebService via an auto-generated C# proxy object. The method I'm calling can be long running, and sometimes times out. I get different errors back, sometimes I get a System.Net.WebException
or a System.Web.Services.Protocols.SoapException
. These exceptions have properties I can interrogate to find the specific type of error from which I can display a human-friendly version of to the user.
我正在使用 C# 并通过自动生成的 C# 代理对象连接到 WebService。我正在调用的方法可能会长时间运行,有时会超时。我得到不同的错误,有时我得到 aSystem.Net.WebException
或 a System.Web.Services.Protocols.SoapException
。这些异常具有我可以查询的属性,以查找特定类型的错误,从中我可以向用户显示人性化的版本。
But sometimes I just get an InvalidOperationException
, and it has the following Message. Is there any way I can interpret what this is without digging through the string for things I recognize, that feels very dirty, and isn't internationalization agnostic, the error message might come back in a different language.
但有时我只是得到一个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.
-->
--.
Edit: I have a try-catch around the method on the web-server. I have debugged it, and the web-server method returns (after a minute or so) without any exception. I also added an unhandled exception handler in the web service and a breakpoint there wasn't hit. As soon as the web-service returns, I get this error in the client instead of the result I expected.
编辑:我在网络服务器上尝试捕获该方法。我已经调试了它,并且 web-server 方法毫无例外地返回(大约一分钟后)。我还在 Web 服务中添加了一个未处理的异常处理程序,并且没有命中一个断点。一旦 Web 服务返回,我就会在客户端中收到此错误,而不是我预期的结果。
回答by Vinko Vrsalovic
That means that your consumer is expecting XML from the webservice but the webservice, as your error shows, returns HTML because it's failing due to a timeout.
这意味着您的使用者期望来自 Web 服务的 XML,但 Web 服务(如您的错误所示)返回 HTML,因为它因超时而失败。
So you need to talk to the remote webservice provider to let them know it's failing and take corrective action. Unless you are the provider of the webservice in which case you should catch the exceptions and return XML telling the consumer which error occurred (the 'remote provider' should probably do that as well).
因此,您需要与远程 Web 服务提供商交谈,让他们知道它失败并采取纠正措施。除非您是网络服务的提供者,在这种情况下,您应该捕获异常并返回 XML,告诉消费者发生了哪个错误(“远程提供者”也应该这样做)。
回答by Seibar
This is happening because there is an unhandled exception in your Web service, and the .NET runtime is spitting out its HTML yellow screen of death server error/exception dump page, instead of XML.
发生这种情况是因为在您的 Web 服务中存在未处理的异常,并且 .NET 运行时正在吐出其死亡服务器错误/异常转储页面的 HTML 黄屏,而不是 XML。
Since the consumer of your Web service was expecting a text/xml header and instead got text/html, it throws that error.
由于您的 Web 服务的使用者期望得到 text/xml 标头,而得到的是 text/html,因此它会抛出该错误。
You should address the cause of your timeouts (perhaps a lengthy SQL query?).
您应该解决超时的原因(也许是冗长的 SQL 查询?)。
Also, checkout this blog poston Jeff Atwood's blog that explains implementing a global unhandled exception handler and using SOAP exceptions.
回答by bastos.sergio
The webserver is returning an http 500 error code. These errors generally happen when an exception in thrown on the webserver and there's no logic to catch it so it spits out an http 500 error. You can usually resolve the problem by placing try-catch blocks in your code.
网络服务器返回一个 http 500 错误代码。这些错误通常发生在 Web 服务器上抛出异常并且没有逻辑可以捕获它时,因此它会吐出 http 500 错误。您通常可以通过在代码中放置 try-catch 块来解决该问题。
回答by Erup
Is your webservice configured correctly in IIS? The pool its using, the version of ASP.NET (2.0) is set? Can you browse the .asmx?
您的 Web 服务是否在 IIS 中正确配置?其使用的池,ASP.NET (2.0) 的版本设置?你能浏览 .asmx 吗?
Talking about exceptions, try to put an try-catch block in the line that access your webservice. Put and catch(System.Web.Services.Protocolos.SoapException).
谈到异常,尝试在访问您的 web 服务的行中放置一个 try-catch 块。放置并捕获(System.Web.Services.Protocolos.SoapException)。
Also, you can set a Timeout for your webservice object.
此外,您可以为您的网络服务对象设置超时。
回答by Mhoque
If you are using .NET version 4.0. the validateRequestion is turned on by default for all the pages. in previous versions 1.1 and 2.0 it was only for aspx page. You can turn the default validation off. In that case you have to do the due diligence and make sure that the data is clean. Use HtmlEncode. Do the following to turn the validation off
如果您使用的是 .NET 4.0 版。默认情况下,所有页面的 validateRequestion 都是打开的。在以前的版本 1.1 和 2.0 中,它仅适用于 aspx 页面。您可以关闭默认验证。在这种情况下,您必须进行尽职调查并确保数据干净。使用 HtmlEncode。执行以下操作以关闭验证
In the web.config add the following lines for system.web
在 web.config 中为 system.web 添加以下行
<httpRuntime requestValidationMode="2.0" />
and
和
<pages validateRequest="false" />
You can read more about this http://www.asp.net/learn/whitepapers/aspnet4/breaking-changesalso http://msdn.microsoft.com/en-us/library/ff649310.aspx
你可以阅读更多关于这个http://www.asp.net/learn/whitepapers/aspnet4/break-changes也http://msdn.microsoft.com/en-us/library/ff649310.aspx
Hope this helps.
希望这可以帮助。
回答by oekstrem
I had this happen as a result of a configuration error in web.config. Checking the connection string etc might be the answer for the time out.
由于 web.config 中的配置错误,我发生了这种情况。检查连接字符串等可能是超时的答案。
回答by Asim Olmez
Delete web.config file and insert again. http://forums.asp.net/post/916808.aspx
删除 web.config 文件并再次插入。http://forums.asp.net/post/916808.aspx
回答by xameeramir
I had got this error after changing the web service return type and SoapDocumentMethod.
更改 Web 服务返回类型和SoapDocumentMethod后出现此错误。
Initially it was:
最初是这样的:
[WebMethod]
public int Foo()
{
return 0;
}
I decided to make it fire and forget typelike this:
我决定让它着火并忘记这样的类型:
[SoapDocumentMethod(OneWay = true)]
[WebMethod]
public void Foo()
{
return;
}
In such cases, updating the web reference helped.
在这种情况下,更新 Web 参考会有所帮助。
To update a web service reference:
要更新 Web 服务引用:
- Expand solution explorer
- Locate Web References- this will be visible only if you have added a web service reference in your project
- Right click and click update web reference
- 展开解决方案浏览器
- 找到Web 引用- 只有在您的项目中添加了 Web 服务引用时,这才可见
- 右键单击并单击更新网络参考
回答by Phi
The problem I had was related to SOAP version. The asmx
service was configured to accept both versions, 1.1 and 1.2, so, I think that when you are consuming the service, the client or the server doesn't know what version resolve.
我遇到的问题与 SOAP 版本有关。该asmx
服务被配置为接受 1.1 和 1.2 两个版本,因此,我认为当您使用该服务时,客户端或服务器不知道解析哪个版本。
To fix that, is necessary add:
要解决这个问题,有必要添加:
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");
}
Where wsWebService
is the name of the class generated as a reference.
wsWebService
作为引用生成的类的名称在哪里。