C#ASMX服务抛出内容类型为text / html;响应消息的charset = UTF-8与内容类型错误不匹配

时间:2019-05-06 15:38:18

标签: c# web-services asmx

我已经通过右键单击根->添加服务引用,为我的项目添加了ASMX服务引用。

我的web.config文件中有这样的内容:

<system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="xxx" />
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="serviceaddress"
        binding="basicHttpBinding" bindingConfiguration="xxx"
        contract="xxx" name="xxx" />
    </client>
  </system.serviceModel>

此服务的方法可以接收带有用户名的string并验证其是否存在。

问题是我正在Postman上对其进行测试,并且返回以下错误消息:

The content type text/html; charset=UTF-8 of the response message does not match the content type

我已经检查了其他类似这样的帖子,但找不到解决方案。

这是我调用的抛出错误的方法:

public static List<UserInformation> GetUsersByUserName(string userName)
        {
            try
            {
                var usersServiceClient = new LDapServicesSoapClient();
                var requestMessage = new LDapUserNameLookupRequest();
                requestMessage.UserName = userName;
                requestMessage.AccessKey = "secretkey";
                var response = usersServiceClient.LDapGetUserByUserName(requestMessage);
                return response.Users.ToList();
            }
            catch (CommunicationException e)
            {
                if (e.InnerException is QuotaExceededException)
                {
                    throw new Exception("We have found many users, please write another filter");
                }
                else
                {
                    throw new Exception(e.Message, e);
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

将此配置添加到我的web.config文件中可以达到神奇的效果:

 <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="LDapServicesSoap" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" bypassProxyOnLocal="false" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
            <message clientCredentialType="Certificate" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="address"
        binding="basicHttpBinding" bindingConfiguration="LDapServicesSoap"
        contract="LDapServices.LDapServicesSoap" name="LDapServicesSoap" />
    </client>
  </system.serviceModel>