【发布时间】:2019-12-21 07:14:25
【问题描述】:
远程服务器返回错误:(415)无法处理消息,因为内容类型“应用程序/xml”不是预期的类型“应用程序/soap + xml”;字符集 = utf-8 '
我只是尝试启动我的自我主机。我有 2 个带有基本身份验证的端点。所以我不得不为此使用 wsHttpBinding。 CreateUser 端点应该使用 XML 格式和 RemoveUser 端点 - json 格式。
我附加了我的 selfhost 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();
【问题讨论】:
-
按照错误的说法,发送正确的内容类型标头。或者更好的是,生成一个 WCF 客户端。
-
我无法生成客户端。 VS 没有看到我的合同 :(