001
001

Reputation: 65205

Create a SOAP header?

How do you add create a SOAP web service header?

Example

<soap:Header>
    <myHeader xmlns="https://www.domain.com">
        <Username>string</Username>
        <Password>string</Password>
    </myHeader>
</soap:Header>

source code

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;


namespace TestWebServices
{
    /// <summary>
    /// Summary description
    /// </summary>
    [WebService(Namespace = "https://Test.com")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Testing : System.Web.Services.WebService
    {

        [WebMethod]
        public string GetTestValue()
        {


            return "xyz";
        }
    }

}

Upvotes: 3

Views: 15237

Answers (4)

Kev
Kev

Reputation: 119846

How about:

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

There's more on the subject here:

Using SOAP Headers (MSDN - archived)

Defining and Processing SOAP Headers (MSDN)

Upvotes: 4

viggity
viggity

Reputation: 15237

If you need fine grain control over how the soap header xml gets rendered (happens when interfacing with a webservice written with java), you can always override all rendering by implementing IXmlSerializable

[XmlRoot("customHeader", Namespace = "http://somecompany.com/webservices/security/2012/topSecret")]
public class customHeader: SoapHeader, IXmlSerializable
{
    public customHeader()
    {
        Actor = "http://schemas.xmlsoap.org/soap/actor/next";
        MustUnderstand = false;
    }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
        //throw new NotImplementedException();
    }

    public void ReadXml(XmlReader reader)
    {
        //throw new NotImplementedException();
    }

    public void WriteXml(XmlWriter writer)
    {
        writer.WriteAttributeString("soap:actor", Actor);
        writer.WriteAttributeString("soap:mustUnderstand", MustUnderstand ? "1" : "0");
        writer.WriteRaw("some encrypted data");
        //get it exactly the way you want it in here without mucking with Xml* property attributes for hours on end
        //writer.WriteElement(...);
    }
}

Upvotes: 4

gotnull
gotnull

Reputation: 27224

EndpointAddressBuilder builder = new EndpointAddressBuilder(client.Endpoint.Address);
AddressHeader header = AddressHeader.CreateAddressHeader("apiKey", "http://tempuri.org", "longapikeyhere");

builder.Headers.Add(header);
client.Endpoint.Address = builder.ToEndpointAddress();

Upvotes: 0

Junior Mayhe
Junior Mayhe

Reputation: 16411

A SOAP message with headers may look something like the following:

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">

  <soap:Header>
    <Username>string</Username>
    <Password>string</Password>
  </soap:Header>

</soap:Envelope>

Upvotes: -1

Related Questions