使用网络服务时出现此错误。我尝试了专家提供的许多解决方案,但没有在我的项目中工作。
错误:
响应消息的内容类型text / html; charset = UTF-8与绑定的内容类型不匹配(text / xml; charset = utf-8)。如果使用自定义编码器,请确保正确实施IsContentTypeSupported方法。
我的web.config文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ping_binding">
<security mode="Transport" />
</binding>
<binding name="ping_binding1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="https://xxxx.com/ping/ping" binding="basicHttpBinding"
bindingConfiguration="ping_binding" contract="NiprWebService.pingIF"
name="ping" />
</client>
</system.serviceModel>
</configuration>
C#代码
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
using (var ws = new NiprWebService.pingIFClient())
{
using (new OperationContextScope(ws.InnerChannel))
{
var requestMessage = new HttpRequestMessageProperty();
string authInfo = USER + ":" + PASS;
authInfo = Convert.ToBase64String(Encoding.Default.GetBytes(authInfo));
requestMessage.Headers.Add("Authorization", "Basic " + authInfo);
requestMessage.Headers.Add("Accept", "text/xml");
requestMessage.Headers.Add("ContentType", "text/xml; charset=utf-8");
//requestMessage.Headers.Add("Method", "POST");
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] =
requestMessage;
}
var gw = new NiprWebService.gateway();
var pr = ws.gateway(gw);
Console.WriteLine(pr.available);
}