繁体   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 '

远程服务器返回错误:(415)由于内容类型'application / xml'不是预期的类型'application / soap + xml,因此无法处理该消息; 字符集= utf-8'

我只是尝试启动我的自助主机。 我有2个具有基本身份验证的端点。 因此,我必须为此使用wsHttpBinding。 CreateUser端点应使用XML格式,而RemoveUser端点应为json格式。

我附加了我的自托管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

[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);

客户端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();

当我们使用wshttpbinding创建WCF服务时,该服务基于Web服务规范化,并使用简单对象访问协议进行通信。 我们可以使用提琴手检查通讯详细信息。
在此处输入图片说明
内容类型是Application / soap + xml而不是Application / xml,并且请求正文格式基于SOAP信封。
https://zh.wikipedia.org/wiki/SOAP
这种Web服务称为SOAP Web服务。 通常,我们使用客户端代理类来调用服务。

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

                throw;
            }

https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/accessing-services-using-a-wcf-client
调用服务的方式通常适用于Restful风格的服务。
https://docs.microsoft.com/zh-cn/azure/architecture/best-practices/api-design
实例化HttpClient类并自定义请求主体。 在这种情况下,我们应该在WCF中使用WebHttpBinding创建服务。 请参考我的回复。
如何通过Jquery调用C#WCF服务来修复“ ERR_ABORTED 400(错误请求)”错误?
请随时告诉我是否有什么我可以帮助的。

更新。

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

将证书绑定到端口(Powershell命令)。

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

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

暂无
暂无

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

相关问题 HTTP 415 无法处理消息,因为内容类型为“application/json; charset=utf-8&#39; 不是预期的类型 &#39;text/xml; 字符集=utf-8&#39; 无法处理消息,因为内容类型为 &#39;application/json; charset=utf-8&#39; 不是预期的类型 &#39;text/xml; 字符集=utf-8&#39; HTTP / 1.1 415无法处理消息,因为内容类型为&#39;application / json; charset = utf-8&#39;不是预期的类型&#39;text / xml; 字符集= UTF-8&#39; WCF错误:(415)内容类型&#39;application / x-www-form-urlencoded&#39;不是预期类型&#39;application / soap + xml; 字符集= UTF-8&#39; WCF成员资格提供程序引发错误:内容类型&#39;application / json; charset = utf-8&#39;不是预期的类型&#39;application / soap + xml; 字符集= UTF-8&#39; 响应消息的内容类型application / xml; charset = utf-8与绑定的内容类型不匹配(text / xml; charset = utf-8) 响应消息的内容类型 application/xml;charset=utf-8 与绑定的内容类型(text/xml; charset=utf-8)不匹配,WCF wcf +响应消息的内容类型text / html与绑定的内容类型不匹配(application / soap + xml; charset = utf-8) 收到错误消息,客户端发现响应内容类型为&#39;text / html; charset = utf-8”,但预期为“ application / soap + xml”? WCF SOAP服务无法处理该消息,因为它发送多部分消息并且需要&#39;text / xml; charset = utf-8&#39;
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM