Home > Windows Phone 7 > WP7 SOAP Web Service with Custom Headers

WP7 SOAP Web Service with Custom Headers

Evet, page rank lazim gençler. İngilizce yazalım, sevgili yabancı arkadaşlarımız da algılasınlar. Hem zaten bizler zeki insanlarız, İngilizce’de nedir yani?

Fellows! Today I am going to show you how you can add your custom headers to your web service requests. I have searched for it for a while in the past, lost my hope, forced my customers to write a new web service with a naive authentication.

But the thing we are going to accomplish is not easy at all.
stringstring
In here we see the soap envelope that we are going to send to get a response. I added the web service with Service References -> Add.

So this little cutie requires an auth header. This header requires Username and Password. So let’s create a class contains those info.

 

So, the order was important in my case, I added it. Those XML Attributes were supposed to work, I did not want any namespaces on elements, but not working, darn.

So we create a static instance of our credentials;

That’s also finished.Only thing we need is to create the header and append to the request.We are going to call a SOAP web service, generated from Visual Studio, so we are going to create it’s client.

Then we need a event handler

client.BolgeSorguCompleted += new EventHandler(client_BolgeSorguCompleted);

Here it is.So the modification we are going to make must be within a OperationContextScope. We create it from the channel of the client. This is how it goes:

AuthHeader is the name of our header, tempuri is the namespace, service credential is the sh*t.So that’s how it goes fellows. Have fun with your custom credentials.

  1. Peggy
    April 10th, 2012 at 18:52 | #1

    Amazing!

  2. KMaj
    June 19th, 2012 at 10:05 | #2

    Thanx for this nice article.
    But when ever I try to connect it return invalid password/username.
    But my Password,Username,namesapce is right

    What I am missing

  3. pabitra
    July 19th, 2012 at 06:37 | #3

    I have a webservice which has soap header like sa:

    string
    string

    When I was trying to consume this web service by the following c# code from windows Phone7:

    // the credential class
    public class ServiceCredential
    {
    [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.None)]
    [DataMember(Order = 1)]
    public string Password;

    [DataMember(Order = 2)]
    [XmlAttribute(Form = System.Xml.Schema.XmlSchemaForm.None)]
    public string Username;

    }

    public static ServiceCredential serviceCredential = new ServiceCredential() { Username = “iphoneservs”, Password = “sdfsdfssdfsdf” };

    using (OperationContextScope scope = new OperationContextScope(client.InnerChannel))
    {
    MessageHeader header = MessageHeader.CreateHeader(“AuthHeader”, “http://tempuri.org/”, ServiceCredential);
    OperationContext.Current.OutgoingMessageHeaders.Add(header);
    client.BolgeSorguAsync();
    }

    The result as:
    The service got username and Password as null. But AuthHeader object not null.

    Can any body tell me what did I wrong? Please help me.

    • alper
      July 20th, 2012 at 11:03 | #4

      Well, you gotta change your Authentication Header name (which was AuthHeader in my case), Username and Password, and use OperationContextScope in every web service call, then you should be ok.

      Check your requests and responses with Fiddler (a tool), and see if they are looking okay.

  1. No trackbacks yet.