zoukankan      html  css  js  c++  java
  • 解决.NET WebService引用后添加HTTP Header的问题

    麻蛋,搜索了好久,找到的都是对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");
                    }
                }
            }
    

      亲测,第二种可用。

  • 相关阅读:
    为什么MySQL死锁检测会严重降低TPS
    OneProxy FAQ 之proxy-user-list
    OneProxy主从延迟检测
    MySQL和OneSQL并行插入性能对比
    天下文章一大抄,你抄我来我抄它
    卖软件的尴尬
    死锁检测为什么耗时?
    28岁的我
    突然想把一生都奉献给MySQL
    如何确定编码风格?
  • 原文地址:https://www.cnblogs.com/tlzzu/p/5819918.html
Copyright © 2011-2022 走看看