【问题标题】:C# ASMX Service throwing The content type text/html; charset=UTF-8 of the response message does not match the content type errorC# ASMX Service throwing 内容类型 text/html; charset=UTF-8 的响应信息与内容类型不匹配错误
【发布时间】:2019-05-06 15:38:18
【问题描述】:

我已通过右键单击根目录为我的项目添加了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);
                }
            }
        }

【问题讨论】:

  • 通常,当调用一个期望响应类型但以另一种响应类型(在本例中为 HTML)的服务时,会引发此错误。通常,如果服务配置为返回正确的“非 html”响应,则当服务的内部调用本身引发异常时会出现此错误,从而导致 500 错误 html 页面描述的 500 状态代码。尝试捕捉 usersServiceClient.LDapGetUserByUserName(requestMessage); 的响应获取 html 并将其保存在 .html 文件中,然后打开它,您可能会收到服务错误(如果我知道它被抛出的地方)
  • 我听不懂,我需要学习如何使用 fiddler。
  • 如果您可以访问 LDap 服务器,请尝试在 EventViewer 中查看日志,如果有日志系统,则在服务本身中查看日志

标签: c# web-services asmx


【解决方案1】:

将此配置添加到我的 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>

【讨论】:

    猜你喜欢
    • 2011-07-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2015-03-02
    • 1970-01-01
    • 1970-01-01
    • 2017-07-14
    相关资源
    最近更新 更多