我需要將自定義標頭附加到我的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");
}
}
HttpWebRequest.ContentType爲null – John 2010-11-16 17:30:21