2010-11-16 67 views
0

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

因此,这里的示例代码:

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"); 
    } 
} 

回答

0

如果你打破你从你的基类取请求刚过,什么是WebRequest的内容类型说呢?

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

+0

HttpWebRequest.ContentType为null – John 2010-11-16 17:30:21

1

当你看到类型为“text/html”的响应时,通常是一个错误页面,试图告诉你什么是错误的。使用Fiddler或其他来查看错误页面试图告诉你什么。

相关问题