无法使用wsHttpBinding在WS Web服务客户端中添加(自定义)http标头吗? [英] Add (custom) http headers in WS Webservice-Client using wsHttpBinding not possible?

查看:110
本文介绍了无法使用wsHttpBinding在WS Web服务客户端中添加(自定义)http标头吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

也许sbdy可以帮助我.我需要在我的webserice客户端的Web服务调用中添加一个http标头(服务器需要有一个用户代理).我找到了一些代码片段,该片段如何使用IClientMessageInspector界面执行此操作.这很好 如果正在使用basicHttpBinding配置.但是,一旦我切换到wsHttpBinding配置,它就会失败.

Maybe sbdy can help me out. I need to add a http header (the server requires to have a user-agent present) to a web-service call of my webserice client. I found some code snippets how to do this using IClientMessageInspector interface.  This works fine if am using a basicHttpBinding configuration. But as soon as I switch to a wsHttpBinding configuration it fails.

调用IClientMessageInspector的BeforeSendRequest方法,在Message属性中存在httpRequestMesageObject,并且可以在此处操纵标头.但是,它们被完全忽略了!

The BeforeSendRequest Method of IClientMessageInspector is called, the httpRequestMesageObject is present in the properties of the Message and the Headers can be manipulated there. But, they are completely ignored!

有人知道如何在基于wsHttpBinding的ws Web服务客户端的客户端调用中添加http标头吗?

Does anybody know how to add a http header to a client call of ws webservice client based on wsHttpBinding???

我正在使用.Net 4.0

I am using .Net 4.0

提前谢谢!

兄弟

Sven Weiberg

Sven Weiberg

推荐答案

我尝试了相同的方案,但效果很好(请参见下面的代码).您可以查看您的代码是否在做其他事情来尝试查找问题?

I tried the same scenario, and it worked out fine (see code below). Can you see if your code is doing something different to try to track down the problem?


  public class Post_69ebfa7c_8a73_4fd1_adf3_d9deb863efad
  {
    [ServiceContract]
    public interface ITest
    {
      [OperationContract]
      string Echo(string text);
    }
    public class Service : ITest
    {
      public string Echo(string text)
      {
        HttpRequestMessageProperty prop = (HttpRequestMessageProperty)OperationContext.Current.IncomingMessageProperties[HttpRequestMessageProperty.Name];
        Console.WriteLine("In server, HTTP headers:");
        foreach (string headerName in prop.Headers.AllKeys)
        {
          Console.WriteLine(" {0}: {1}", headerName, prop.Headers[headerName]);
        }

        return text;
      }
    }
    class MyClientInspector : IEndpointBehavior, IClientMessageInspector
    {
      public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
      {
      }

      public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
      {
        clientRuntime.MessageInspectors.Add(this);
      }

      public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
      {
      }

      public void Validate(ServiceEndpoint endpoint)
      {
      }

      public void AfterReceiveReply(ref Message reply, object correlationState)
      {
      }

      public object BeforeSendRequest(ref Message request, IClientChannel channel)
      {
        HttpRequestMessageProperty prop;
        if (request.Properties.ContainsKey(HttpRequestMessageProperty.Name))
        {
          prop = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
        }
        else
        {
          prop = new HttpRequestMessageProperty();
          request.Properties.Add(HttpRequestMessageProperty.Name, prop);
        }

        prop.Headers.Add("X-MyCustom", "HeaderValue");
        return null;
      }
    }
    static Binding GetBinding()
    {
      return new WSHttpBinding();
    }
    public static void Test()
    {
      string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
      ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
      ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
      host.Open();
      Console.WriteLine("Host opened");

      ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress));
      factory.Endpoint.Behaviors.Add(new MyClientInspector());
      ITest proxy = factory.CreateChannel();
      Console.WriteLine(proxy.Echo("Hello"));

      ((IClientChannel)proxy).Close();
      factory.Close();

      Console.Write("Press ENTER to close the host");
      Console.ReadLine();
      host.Close();
    }
  }


这篇关于无法使用wsHttpBinding在WS Web服务客户端中添加(自定义)http标头吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆