Wednesday, March 19, 2014

.NET 4.0 Consume weblogic web service

Recently i have a project which need to consume a weblogic web service using .NET framework 4.0. At first i think it were similar to calling other java web service, which adding service reference and then all is done, but when i am working on it, i soon find that there is a problem on the authentication part.

I have the sample call which write in Java and the code to do the authentication is like following


        BindingProvider bindingProvider = (BindingProvider)soapService;
        bindingProvider.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, username);
        bindingProvider.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, password);


Poor that in .NET framework 4.0, there seems no similar stuff with that. So what it actually doing on these few lines? In this forum, i find a solution at the bottom which overrides the service client to add the header, but it is for .NET 2.0 webservice
http://bytes.com/topic/net/answers/427818-calling-web-service-hosted-web-logic-server-c-soap-client

So my main target now is add the http header in .NET 4.0? and finally find the solution here
http://stackoverflow.com/questions/11509053/adding-http-headers-to-a-service-reference-service-method

Combining these two link, the solution would be
1. create the two class HttpHeaderMessageInspector and HttpHeadersEndpointBehavior from the second link
2. use the following code on your soap client


        Dim code() As Byte = System.Text.Encoding.ASCII.GetBytes(String.Format("{0}:{1}", userName, password))
        Dim b64str As String = Convert.ToBase64String(code)
        Dim header As New Dictionary(Of String, String)
        header.Add("Authorization", "Basic " & b64str)
        soapClient.Endpoint.Behaviors.Add(New HttpHeadersEndpointBehavior(header))


and then when call, it will do the authentication successfully without error