gpt4 book ai didi

WCF ProtocolException 接收 html

转载 作者:行者123 更新时间:2023-12-02 08:48:30 27 4
gpt4 key购买 nike

从 WCF 访问 DotNet 的方法时出现以下错误

内容类型text/html;响应消息的 charset=utf-8 与绑定(bind)的内容类型不匹配(text/xml;charset=utf-8)。如果使用自定义编码器,请确保正确实现 IsContentTypeSupported 方法。响应的前 1024 个字节是:' 运行时错误 正文{字体系列:“Verdana”;字体粗细:正常;字体大小:.7em;颜色:黑色;} 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 {字体粗细:粗体;颜色:黑色;文字装饰:无;} .version {颜色:灰色;} .error {margin-bottom: 10px;} .expandable { 文本装饰:下划线;字体粗细:粗体;颜色:海军蓝;光标:手; }

<body bgcolor="white">

<span><H1>Server Error in '/ORSXMLWCFServiceNew' Application.<hr width=100% size=1 color=silver></H1>

<h2> <i>Runtime Error</i> </'.

请帮助我。

最佳答案

我找到了一种获得完整响应的方法,而不仅仅是无用的 1024 字节...

using (var client = new Service1Client())
{
try
{
Console.WriteLine(client.DoWork());
}
catch (ProtocolException e)
{
var webException = e.InnerException as WebException;

var responseString = webException.ExtractResponseString();

if (string.IsNullOrEmpty(responseText))
Console.WriteLine(e);
else
Console.WriteLine(responseString);
}
catch (Exception e)
{
Console.WriteLine(e);
}
}

使用以下辅助方法的地方

public static string ExtractResponseString(this WebException webException)
{
if (webException == null || webException.Response == null)
return null;

var responseStream = webException.Response.GetResponseStream() as MemoryStream;

if (responseStream == null)
return null;

var responseBytes = responseStream.ToArray();

var responseString = Encoding.UTF8.GetString(responseBytes);
return responseString;
}

查看我的博客了解更多详情http://mnaoumov.wordpress.com/2012/09/28/wcf-protocolexception/

关于WCF ProtocolException 接收 html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10549261/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com