简体   繁体   English

内容类型 text/xml; 响应消息的 charset="utf-8" 与绑定的内容类型不匹配 (text/xml; charset=utf-8)

[英]The content type text/xml; charset=“utf-8” of the response message does not match the content type of the binding (text/xml; charset=utf-8)

Please note that this question pertains to the .NET Core implementation of WCF Connected Services.请注意,此问题与 WCF 连接服务的 .NET Core 实现有关。

I am porting a regular .NET WCF client over to .NET Core, but I ran into this issue:我正在将一个普通的 .NET WCF 客户端移植到 .NET Core,但我遇到了这个问题:

The content type text/xml; charset="utf-8" of the response message does
not match the content type of the binding (text/xml; charset=utf-8).

If using a custom encoder, be sure that the IsContentTypeSupported method is
implemented properly. The first 1024 bytes of the response were: 
'<?xml version='1.0' encoding='UTF-8'?> [...]

The response indeed contains the quotes:响应确实包含引号:

HTTP/1.1 200 Ok
content-type: text/xml; charset="utf-8"

I never did anything special to handle this in WCF proper.我从来没有做任何特别的事情来在 WCF 中正确处理这个问题。 Is this a bug in the .NET Core version, or is it just really specific about the content type (utf-8 vs "utf-8")?这是 .NET Core 版本中的错误,还是仅针对内容类型(utf-8 与“utf-8”)进行了具体说明?

How can I change the expected content type to match the service I'm calling?如何更改预期的内容类型以匹配我正在调用的服务? (I have no control over that, but I can copy and alter the WSDL if needed). (我无法控制它,但如果需要,我可以复制和更改 WSDL)。

I'm using a svcutil-generated client.我正在使用 svcutil 生成的客户端。 (Connected Service) (连接服务)

It would indeed seem that the .NET Core version is more picky about this. .NET Core 版本似乎确实对此更加挑剔。 In any case, I managed to solve it using a Custom Encoder.无论如何,我设法使用自定义编码器解决了它。

I blatently stole the CustomTextMessageEncoder from Github .我公然从Github窃取了 CustomTextMessageEncoder。 I added the following method:我添加了以下方法:

public override bool IsContentTypeSupported(string contentType)
{
    return true;
}

And stole CustomTextMessageBindingElement and CustomTextMessageEncoderFactory from the same place.并从同一个地方窃取了CustomTextMessageBindingElementCustomTextMessageEncoderFactory

I added them by creating a custom binding (basicBinding is the binding I had before):我通过创建自定义绑定来添加它们(basicBinding 是我之前的绑定):

var customBindingElement = new CustomTextMessageBindingElement("UTF-8", "text/xml", MessageVersion.Soap11);
var binding = new CustomBinding(basicBinding);
binding.Elements.RemoveAt(0);
binding.Elements.Insert(0, customBindingElement);
var client = (T2)Activator.CreateInstance(typeof(T), binding, address);

I use Activator as I generate my proxies dynamically.我使用 Activator 来动态生成代理。 Just replace with a call to the WCF generated client.只需替换为对 WCF 生成的客户端的调用。

Quite a lot of work for two misplaced quotes :D两个错位的报价需要做很多工作:D

I was getting the exact same error after converting a WCF Client to dotnet core.将 WCF 客户端转换为 dotnet 核心后,我遇到了完全相同的错误。 I have to refactor some.我必须重构一些。 I replaced my binding code to this.我将绑定代码替换为此。 Note WsBinding isn't native in dotnet core.注意 WsBinding 在 dotnet 核心中不是原生的。

https://github.com/dotnet/wcf/issues/1370 https://github.com/dotnet/wcf/issues/1370

I had the same problem also.我也有同样的问题。 There might be an issue with security-related or content-type format differences.可能存在与安全相关或内容类型格式差异的问题。 I got over the problem by ignoring ServiceReference bindings.我通过忽略 ServiceReference 绑定解决了这个问题。 You should directly call with HttpWebRequest As a sample;你应该直接调用HttpWebRequest作为示例;

  private string GetWebService(string token, int testId, string phone)
    {
        try
        {
            var serviceUrl = "https://test.com/Soap";

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(serviceUrl);
            httpWebRequest.ContentType = "text/xml";
            httpWebRequest.Method = "POST";

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {

                var xmlData = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:doc=\"http://test.com\">" +
                "   <soapenv:Header/>" +
                "   <soapenv:Body>" +
                "      <doc:AskForStackOverFlow>" +
                "         <doc:token>" + token + "</doc:token>" +
                "         <doc:testId>" + testId+ "</doc:testId>" +
                "         <doc:phone>" + phone + "</doc:phone>" +
                "         <doc:startTime>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "</doc:startTime>" +
                "      </doc:AskForStackOverFlow>" +
                "   </soapenv:Body>" +
                "</soapenv:Envelope>";

                streamWriter.Write(xmlData);
            }

            var xmlResult = "";
            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                xmlResult = streamReader.ReadToEnd();
            }

            if (string.IsNullOrEmpty(xmlResult))
                return "";


            var result = "";
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xmlResult);
            XmlNodeList nodes = doc.GetElementsByTagName("ns1:AskForStackOverFlowResult");
            foreach (XmlNode item in nodes)
            {
                result = item.InnerText;
            }

            return result;

        }
        catch (Exception ex)
        {
            return ex.Message;
        }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 响应消息的内容类型application / xml; charset = utf-8与绑定的内容类型不匹配(text / xml; charset = utf-8) - The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) 响应消息的内容类型 application/xml;charset=utf-8 与绑定的内容类型(text/xml; charset=utf-8)不匹配,WCF - The content type application/xml;charset=utf-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8), WCF 内容类型text / html; charset =响应消息的UTF-8与绑定的内容类型不匹配(text / xml; charset = utf-8) - The content type text/html; charset=UTF-8 of the response message does not match the content type of the binding (text/xml; charset=utf-8) 响应消息的内容类型文本/纯文本与绑定的内容类型不匹配(文本/ xml; charset = utf-8) - The content type text/plain of the response message does not match the content type of the binding (text/xml; charset=utf-8) wcf +响应消息的内容类型text / html与绑定的内容类型不匹配(application / soap + xml; charset = utf-8) - wcf + The content type text/html of the response message does not match the content type of the binding (application/soap+xml; charset=utf-8) WCF http绑定错误:内容类型text / html与绑定的内容类型不匹配(text / xml; charset = utf-8) - WCF http binding error: The content type text/html does not match the content type of the binding (text/xml; charset=utf-8) WCF 服务客户端:内容类型text/html; 响应消息的 charset=utf-8 与绑定的内容类型不匹配 - WCF Service Client: The content type text/html; charset=utf-8 of the response message does not match the content type of the binding HTTP 415 无法处理消息,因为内容类型为“application/json; charset=utf-8&#39; 不是预期的类型 &#39;text/xml; 字符集=utf-8&#39; - HTTP 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' 无法处理消息,因为内容类型为 &#39;application/json; charset=utf-8&#39; 不是预期的类型 &#39;text/xml; 字符集=utf-8&#39; - Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' C#ASMX服务抛出内容类型text / html; charset =响应消息的UTF-8与内容类型错误不匹配 - C# ASMX Service throwing The content type text/html; charset=UTF-8 of the response message does not match the content type error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM