麻蛋,搜索了好久,找到的都是對soap header的操作,不是對WebService的HTTP Header的操作,這是兩種不同的概念,平常我們發起的WebService請求走的都是http通信協議,POST方式的請求,請求體是發送對象的SOAP結構。算了直接上答案。只不過還是英語不好
http://stackoverflow.com/questions/897782/how-to-add-custom-http-header-for-c-sharp-web-service-client-consuming-axis-1-4
上面是源地址,貌似有兩種方法,一種是排名第一的
添加一下代碼:
protected override System.Net.WebRequest GetWebRequest(Uri uri)
{
System.Net.WebRequest request = base.GetWebRequest(uri);
request.Headers.Add("myheader", "myheader_value");
return request;
}
老外真是,愣是不提供完整的demo,費了半天勁,實在找不到該插入那個地方啊,有木有,試遍了自動生成的Reference.cs里的任何位置,都不對,哪位大神指導的,還請指導一下。
第二種:
static void Main(string[] args)
{
//wsAuth:就是自動生成的WebService服務
using (wsAuth.WsAuthenticationClient client = new wsAuth.WsAuthenticationClient())
{
using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
{
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers["ws-lala"] = "111";
httpRequestProperty.Headers["ws-lala11"] = "222";
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
var cc = client.login("tlzzu", "123456");
}
}
}
親測,第二種可用。
