解決.NET WebService引用後添加HTTP Header的問題

麻蛋,搜索了很久,找到的都是對soap header的操做,不是對WebService的HTTP Header的操做,這是兩種不一樣的概念,日常咱們發起的WebService請求走的都是http通訊協議,POST方式的請求,請求體是發送對象的SOAP結構。算了直接上答案。只不過仍是英語很差web

http://stackoverflow.com/questions/897782/how-to-add-custom-http-header-for-c-sharp-web-service-client-consuming-axis-1-4ide

上面是源地址,貌似有兩種方法,一種是排名第一的對象

添加一下代碼:blog

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裏的任何位置,都不對,哪位大神指導的,還請指導一下。string

第二種:io

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

  親測,第二種可用。class

相關文章
相關標籤/搜索