【发布时间】: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");
}
}
【问题讨论】: