【问题标题】:.NET Adding Custom Http Headers to Web Service Proxy throws errors.NET 向 Web 服务代理添加自定义 Http 标头会引发错误
【发布时间】:2011-02-16 19:26:56
【问题描述】:

我需要将自定义标头附加到我的 Web 服务客户端。 为此,我为添加 Web 引用时生成的 SoapHttpClientProtocol 类创建了部分类。但是,当我附加了自定义标头时,我收到了运行时错误。如果未附加自定义标头,一切正常 (当我评论附加标题的代码时)。 但是,如果添加了自定义标头,那么我在运行时收到以下错误: 客户端发现响应内容类型为 'text/html; charset=utf-8',但应为 'text/xml'。 有什么想法吗?

下面是示例代码:

 public partial class SMARTSWS : System.Web.Services.Protocols.SoapHttpClientProtocol 
{
    private NameValueCollection _customHeaders = new NameValueCollection();

    protected override System.Net.WebRequest GetWebRequest(System.Uri Uri)
    {
        // Add authentication cookie to the 
        // this object CookieContainer
        SmartsIVRSecurityManager.SetAuthToken(this);

        // Set Custom Headers
        SetCustomHeaders();

        HttpWebRequest req = (HttpWebRequest)base.GetWebRequest(Uri);
        for (int i = 0; i <= _customHeaders.Count - 1; i++)
        {
            req.Headers.Add(_customHeaders.Keys[i], _customHeaders.GetValues(i).GetValue(0).ToString());
        }
        return req;
    }


    /// <summary>
    /// Set Custom Headers
    /// </summary>
    /// <param name="smToken"></param>
    public void SetCustomHeaders()
    {
        _customHeaders.Add("Version", "1.0");
        _customHeaders.Add("OnBehalfOf", String.Empty);
        _customHeaders.Add("Role", "1");
        _customHeaders.Add("EndPoint", "001");
        _customHeaders.Add("ServiceId", "001");
        _customHeaders.Add("DateTime", String.Empty);
        _customHeaders.Add("ClientApplication", "SmartsIVRService");
        _customHeaders.Add("TraceWebMethod", "false");
        _customHeaders.Add("ClientTouchPoint", "SmartsIVRService");
        _customHeaders.Add("ChannelInfo", "ChannelInfo");
    }
}

【问题讨论】:

    标签: .net service asmx


    【解决方案1】:

    当您看到“text/html”类型的响应时,通常是一个错误页面试图告诉您哪里出了问题。使用 Fiddler 或其他工具查看错误页面试图告诉您的内容。

    【讨论】:

      【解决方案2】:

      如果您在从基类获取请求后立即中断,那么 webrequest 内容类型说明了什么?

      设置为 text/html 还是 text/xml?我想知道你的演员调用是否正在改变类型。

      【讨论】:

      • HttpWebRequest.ContentType 为空
      猜你喜欢
      • 2010-11-21
      • 1970-01-01
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-28
      • 1970-01-01
      • 2013-06-11
      相关资源
      最近更新 更多