简体   繁体   English

无法处理该消息,因为内容类型“ application / xml”不是预期的类型“ application / soap + xml”; 字符集= utf-8'

[英]Cannot process the message because the content type 'application / xml' was not the expected type 'application / soap + xml; charset = utf-8 '

The remote server returned an error: (415) Cannot process the message because the content type 'application / xml' was not the expected type 'application / soap + xml; 远程服务器返回错误:(415)由于内容类型'application / xml'不是预期的类型'application / soap + xml,因此无法处理该消息; charset = utf-8 ' 字符集= utf-8'

I just try to launch my self host. 我只是尝试启动我的自助主机。 I have 2 endpoints with Basic Authentication. 我有2个具有基本身份验证的端点。 So I had to use wsHttpBinding for that. 因此,我必须为此使用wsHttpBinding。 CreateUser endpoint should use XML format and RemoveUser endpoint - json format. CreateUser端点应使用XML格式,而RemoveUser端点应为json格式。

I attached my selfhost app.config, client main function and contract. 我附加了我的自托管app.config,客户端主要功能和合同。

server app.config 服务器app.config

<services>
  <service name="Web.Service.Core.Services.UserContract"
           behaviorConfiguration="AuthBehavior" >
    <endpoint address="CreateUser"
              binding="wsHttpBinding"
              bindingNamespace="http://localhost/Auth/"
              contract="Web.Service.Library.Contracts.IUserContract" />
    <endpoint address="RemoveUser"
              binding="wsHttpBinding"
              contract="Web.Service.Library.Contracts.IUserContract" />

IUserContract.cs IUserContract.cs

[ServiceContract(Namespace = "http://localhost/Auth/", ProtectionLevel = ProtectionLevel.None)]
[XmlSerializerFormat]
public interface IUserContract
{
    [OperationContract]
    [WebInvoke(Method = "POST",
        UriTemplate = "Auth/CreateUser",
        RequestFormat = WebMessageFormat.Xml,
        ResponseFormat = WebMessageFormat.Xml,
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    Response CreateUser(Stream xml);

    [OperationContract]
    [WebInvoke(Method = "POST",
        UriTemplate = "Auth/RemoveUser",
        RequestFormat = WebMessageFormat.Json,
        ResponseFormat = WebMessageFormat.Json,
        BodyStyle = WebMessageBodyStyle.Wrapped)]
    Response RemoveUser(Stream stream);

client main() 客户端main()

var webRequest = (HttpWebRequest) WebRequest.Create(CreateUserUrl);
webRequest.Method = "POST";
webRequest.ContentType = "application/xml";
webRequest.ContentLength = data.Length;
var rqStream = webRequest.GetRequestStream();
rqStream.Write(data, 0, data.Length);
rqStream.Close();
var webResponse = webRequest.GetResponse();
var rsStream = webResponse.GetResponseStream();
var responseXml = new StreamReader(rsStream);
var s = responseXml.ReadToEnd();

When we use wshttpbinding to create WCF service, the service based on the web service speficification, and use Simple Object Access Protocol to communicate. 当我们使用wshttpbinding创建WCF服务时,该服务基于Web服务规范化,并使用简单对象访问协议进行通信。 We could check the communication details by using fiddler. 我们可以使用提琴手检查通讯详细信息。
在此处输入图片说明
The content-type is Application/soap+xml instead of the Application/xml, and the request body format based on SOAP envelop. 内容类型是Application / soap + xml而不是Application / xml,并且请求正文格式基于SOAP信封。
https://en.wikipedia.org/wiki/SOAP https://zh.wikipedia.org/wiki/SOAP
This kind of web service is called SOAP web service. 这种Web服务称为SOAP Web服务。 Ordinarily, we call the service by using the client proxy class. 通常,我们使用客户端代理类来调用服务。

   ServiceReference1.ServiceClient client = new ServiceClient();
            try
            {
                var result = client.GetData();
                Console.WriteLine(result);
            }
            catch (Exception)
            {

                throw;
            }

https://docs.microsoft.com/en-us/dotnet/framework/wcf/accessing-services-using-a-wcf-client https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/accessing-services-using-a-wcf-client
The way you call a service usually applies to Restful-style service. 调用服务的方式通常适用于Restful风格的服务。
https://docs.microsoft.com/en-us/azure/architecture/best-practices/api-design https://docs.microsoft.com/zh-cn/azure/architecture/best-practices/api-design
Instantiate the HttpClient class and custom the request body. 实例化HttpClient类并自定义请求主体。 Under this circumstance, we should use WebHttpBinding in WCF to create the service. 在这种情况下,我们应该在WCF中使用WebHttpBinding创建服务。 Please refer to my reply. 请参考我的回复。
How to fix "ERR_ABORTED 400 (Bad Request)" error with Jquery call to C# WCF service? 如何通过Jquery调用C#WCF服务来修复“ ERR_ABORTED 400(错误请求)”错误?
Feel free to let me know if there is anything I can help with. 请随时告诉我是否有什么我可以帮助的。

Updated. 更新。

class Program
{
    /// <summary>
    /// https webhttpbinding.
    /// </summary>
    /// <param name="args"></param>
    static void Main(string[] args)
    {
        Uri uri = new Uri("https://localhost:4386");
        WebHttpBinding binding = new WebHttpBinding();
        binding.Security.Mode = WebHttpSecurityMode.Transport;
        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic;


        using (WebServiceHost sh = new WebServiceHost(typeof(TestService), uri))
        {
            sh.AddServiceEndpoint(typeof(ITestService), binding, "");
            ServiceMetadataBehavior smb;
            smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
            if (smb == null)
            {
                smb = new ServiceMetadataBehavior()
                {
                    //HttpsGetEnabled = true
                };
                sh.Description.Behaviors.Add(smb);

            }
            Binding mexbinding = MetadataExchangeBindings.CreateMexHttpsBinding();
            sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");


            sh.Opened += delegate
            {
                Console.WriteLine("service is ready");
            };
            sh.Closed += delegate
            {
                Console.WriteLine("service is closed");
            };
            sh.Open();

            Console.ReadLine();

            sh.Close();
        }
    }
}
[ServiceContract]
public interface ITestService
{
    [OperationContract]
    [WebGet(ResponseFormat =WebMessageFormat.Json)]
    string GetResult();
}

[ServiceBehavior]
public class TestService : ITestService
{
    public string GetResult()
    {
        return $"Hello, busy World. {DateTime.Now.ToShortTimeString()}";
    }
}

Bind a certificate to the port(Powershell command). 将证书绑定到端口(Powershell命令)。

    netsh http add sslcert ipport=0.0.0.0:4386 certhash=cbc81f77ed01a9784a12483030ccd497f01be71c App
id='{61466809-CD17-4E31-B87B-E89B003FABFA}'

Result. 结果。
在此处输入图片说明

暂无
暂无

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

相关问题 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' HTTP / 1.1 415无法处理消息,因为内容类型为&#39;application / json; charset = utf-8&#39;不是预期的类型&#39;text / xml; 字符集= UTF-8&#39; - HTTP/1.1 415 Cannot process the message because the content type 'application/json; charset=utf-8' was not the expected type 'text/xml; charset=utf-8' WCF错误:(415)内容类型&#39;application / x-www-form-urlencoded&#39;不是预期类型&#39;application / soap + xml; 字符集= UTF-8&#39; - WCF ERROR: (415) content type 'application/x-www-form-urlencoded' was not the expected type 'application/soap+xml; charset=utf-8' WCF成员资格提供程序引发错误:内容类型&#39;application / json; charset = utf-8&#39;不是预期的类型&#39;application / soap + xml; 字符集= UTF-8&#39; - WCF Membership Provider throws error: content type 'application/json; charset=utf-8' was not the expected type 'application/soap+xml; charset=utf-8' 响应消息的内容类型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 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) 收到错误消息,客户端发现响应内容类型为&#39;text / html; charset = utf-8”,但预期为“ application / soap + xml”? - Getting the error Client found response content type of 'text/html; charset=utf-8', but expected 'application/soap+xml'? WCF SOAP服务无法处理该消息,因为它发送多部分消息并且需要&#39;text / xml; charset = utf-8&#39; - WCF SOAP service cannot process the message because it sends multipart message and expects 'text/xml; charset=utf-8'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM