SOAP Header (credentials) for cosuming web service

Its the optional part of SOAP Message. Instead including meta data (Credentials) in your web signature you can use SOAP header to consume web service.

// SOAP Header class

public class HelloHeader : SoapHeader
{
    public string Username;
    public string Password;
}

    public HelloHeader myHeader;
    [WebMethod]
    [SoapHeader("myHeader")]
    public string HelloWorld()
    {
        if (myHeader == null)
        {
            return "Hello World";
        }
        else
        {
            return "Hello " + myHeader.Username + ". " +
            "
Your password is: " + myHeader.Password;
        }
    }


// Consuming this web service
localhost.HelloHeader objHEader = new WindowsApplication1.localhost.HelloHeader();
localhost.HelloSoapHeader objService = new WindowsApplication1.localhost.HelloSoapHeader();
objHEader.Username = "milind";
objHEader.Password = "mahajan";
objService.HelloHeaderValue = objHEader;
MessageBox.Show( objService.HelloWorld());

Comments

  1. Good Day!

    You know how to consume this webservice for example with jquery ajax passing the Soap Header?

    ReplyDelete

Post a Comment

Popular posts from this blog

How to get motherboard serial number, Processor ID, VolumeSerialNumber using C#?

Fiserv Interview Questions.

AngularJs - Simple Example