zcwry

解决.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里的任何位置,都不对,关键是winform 里面引用完成接口之后还找不到Reference.cs 文件。

第二种方式:最好是把using 去掉,要不出错都不知道哪里报错了。

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

  去掉了using之后的测试实例,亲测有用

public string ces(string xml)
        {
            try
            {
                ceshi.RegistryAddRequestImplServiceClient client = new ceshi.RegistryAddRequestImplServiceClient();
                OperationContextScope scope = new OperationContextScope(client.InnerChannel);
                var httpRequestProperty = new HttpRequestMessageProperty();
                httpRequestProperty.Headers["username"] = "py";
                httpRequestProperty.Headers["password"] = "py";
                OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
                var cc = client.HIPMessageServer("PatientRegistryAddRequest", xml);
            }
            catch (Exception ec)
            {

            }

            return "";
        }

  

posted on 2018-01-30 08:58 蓉宇-轩 阅读(...) 评论(...) 编辑 收藏
 
 

分类:

.net

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-18
  • 2021-05-24
  • 2021-11-19
  • 2022-12-23
猜你喜欢
  • 2021-11-19
  • 2021-09-13
  • 2022-12-23
  • 2022-12-23
  • 2021-10-21
  • 2021-06-18
  • 2021-10-01
  • 2022-12-23
相关资源
相似解决方案