[英]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'
[英]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,客户端主要功能和合同。
<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" />
[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);
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.