Add header information to my Web Service Reference request. RRS feed

  • Question

  • User794943815 posted

    I want to add header information to my Web Service, which I created with basic wizard in Visual Studio 2012.


    I simply clicked "Add Service Reference", gave my link and VS created the code itself. Which class do I have to use to pass the header information of my request? Do I have to override the code that VS created automatically?


    One more note: If you know another way to add a header to a web service, I would like to use it too. I don't have to use automaticaly created codes. (Although it's handy to use them.)

    Monday, December 3, 2012 5:01 AM

All replies

  • User614805505 posted




    You may refer to these 2 links for sample. The important thing is to add a SoapHeader attribute to the web method. Later you can simply generate the proxy from Visual Studio.

    Monday, December 3, 2012 6:52 AM
  • User794943815 posted

    CruzerB, Thank you for your answer.


    I checked the samples, I think both of them shows how to create the credentials of my own, and send to the web service that I created. You create the "username-password" objects yourself, and send information through a client.


    In my case, I don't have such credentials. I just have the link of the web service, and a document tells me that "Before you call the web service, you must add these parameters to the header of your request." From this, I am thinknig that the "header" is a standart node which can be reached from .Net interface..


    Is there a simple code to just adda "key-value" pair to the header of the request?

    Monday, December 3, 2012 7:48 AM
  • User614805505 posted

    The key thing is this class.

    public class UserCredentials : System.Web.Services.Protocols.SoapHeader
        public string userName;
        public string password;

    You actually can customize it by introduce a Dictionary<String, String> property.

    I just have the link of the web service, and a document tells me that "Before you call the web service, you must add these parameters to the header of your request." From this, I am thinknig that the "header" is a standart node which can be reached from .Net interface..

    There must be a manual way to inject a data into soap header just I found this way is much more convenient. Since you can actually define a strong type of your header object by creating a custom class which inherit System.Web.Services.Protocols.SoapHeader. Then generate proxy by using Add Services Reference at Visual Studio. 

    After that, you will found that your web method comes with extra parameter(It actually refer to the custom class that you created at web services). So you can create a new instance of that parameter type and assign it with the data that you want it to be as Soap Header. And pass it to the web method. Thats all. 

    Monday, December 3, 2012 8:40 AM
  • User794943815 posted

    Thanks for your answer.

    Because of my lack of skills, I am having hard time finding the solution. Where exactly should I put the UserCredentials code, so it will appear in the method?

     Here is the code that I call the web service. I send it to be sure we are on the same page. After I add Service Reference, I use these code to call the web service. (MyCompanyService class is the one created automatically by Visual Studio.)


    WebServicePackage.MyCompanyService.MyCompanyServiceClient myCompanyClient = new WebServicePackage.MyCompanyService.MyCompanyServiceClient();

    WebServicePackage.MyCompanyService.MyCompanyInputType input = new WebServicePackage.MyCompanyService.MyCompanyInputType();

                input.request1 = "req1";
                input.request2= "req2";
                WebServicePackage.MyCompanyService.MyCompanyResultType results = myCompanyClient.methodToCall(input);


    Tuesday, December 4, 2012 2:31 AM
  • User614805505 posted

    Is the web services development by you? The header suppose to define at the web services first then only you go and generate the services reference.

    Tuesday, December 4, 2012 3:06 AM
  • User794943815 posted

    No, the Web Service is given by some government company. I have no control over the Web Service.
    The thing is, this can be done at Java. My CoWorker sent me the Java code that works fine:

    public static SOAPHeader getHeader( SOAPEnvelope envelope) throws SOAPException
            String SECURITY_NAMESPACE = "CompanysNameSpace.xsd"; 
            String USERNAMETOKEN_NAMESPACE = "CompanysWebServiceUrl.xsd";
            String MY_USERNAME = "Username_ToPutTheHeader";

            SOAPFactory soapFactory;
            SOAPHeader header = null;

                soapFactory = SOAPFactory.newInstance();
                header = envelope.getHeader(); 
                SOAPElement security = header.addChildElement("Security", "wsse", SECURITY_NAMESPACE);
                SOAPElement usernameToken = security.addChildElement("UsernameToken", "wsse");
                usernameToken.addAttribute(soapFactory.createName("xmlns:wsu"), USERNAMETOKEN_NAMESPACE);
                SOAPElement username = security.addChildElement("Username", "wsse");
                return header;       
            catch (SOAPException e) {

                throw e;       

    So my question is: Which dotNet node sets the exact variable that set by "MY_USERNAME" on the Java code above?

    Tuesday, December 4, 2012 3:49 AM
  • User614805505 posted

    Well, I never do that before but I think I found a thread which has the same issue with you. You have to create a extension class for this purpose. :P

    *Please read until the end. It contains 2 pages.


    Tuesday, December 4, 2012 4:07 AM
  • User794943815 posted

    :) Ok I am reading now.

    Honestly, I found that subject during my search, but never get to the details. Because I saw the SOAP codes and that's my last hope to reach my goal because I already coded whole 7 web services.

    Tuesday, December 4, 2012 4:14 AM
  • User794943815 posted

    Ok i'm done reading.

    So this mate created the whole soap request himself, and put the header through overrided classes as a string value. (Looks like this header is a huge deal even if you use the simplest way... I just can't believe this is that much of a problem.)

    My code calls the Web Service through the code prepared by Visual Studio. Is there any class I can override to send header information through my code?

    Tuesday, December 4, 2012 4:45 AM
  • User-1867322151 posted

    //instantiate the serviceProxy

    var client = new domates.XXX();

    using ( new OperationContextScope( client.InnerChannel))

    var msgHeader = MessageHeader.CreateHeader("SSOToken", "", MyToken);
    var msgHeader2 = MessageHeader.CreateHeader("TCKimlikNo", "", MyTcKimlik);

    Thursday, January 16, 2014 7:44 AM