Integrate PostNL webservice with Dot Net Application c#

Hi Folks,

Recently I had a chance to work with the PostNL webservices. For those who don’t know about it PostNL and their webservices, PostNL is a mail, parcel and e-commerce corporation they provide variety of API to access their services and products. So we can integrate PostNL with our system using those services. Here we’ll see how we can Integrate PostNL webservice with Dot Net Application.

The goal is to integrate their location service with our site in order to find the nearest PostNL pickup location to the customer.

Find the full code in github or  PostNL download.

As I googled it I don’t see any proper .Net implementation helps to access their services. There is a one PHP and it seems it’s hard to understand for .Net developers :). So let’s directly jump in to the programming playground.

PostNL webservices are accessible using SOAP protocol and,hope most of you are familiar with accessing the services by adding service reference to your Visual Studio project.

 

Usually once you added the service reference you should be able to access all methods and, simply call the methods just passing the correct request data. But the problem with PostNL webservice is, we can pass the required filter data in the SOAP body using the objects but the login credentials need to be passed via Header and we need to create the header manually. We can simply create the GetNearestLocationsRequest and pass it to the method as shown below.

The difficult part is integrating the header with login credentials with multi level hierarchy. The way to send those credentials is, add the custom header when creating the endpoints. You can find a custom webservice hearder example here with full explanation. When we create the custom header we have create in away that has the required structure as follows. Refer here for good example.

That’s it, now just call the PostNL provider and relax 😉

 

Please update the username and password in the app.config file. Note the password should be encrypted as mentioned in their documentation.   The encryption must be done with the SHA1 algorithm.

Reference

https://developer.xamarin.com/api/type/System.Xml.XmlTextWriter/

https://blogs.msdn.microsoft.com/mohamedg/2012/10/21/adding-custom-soap-headers-in-wcf/

https://developer.postnl.nl/apis/location-webservice/documentation

https://developer.postnl.nl/apis/location-webservice/how-use

You may also like...

3 Responses

  1. Tsjelle says:

    Thank you very much for this post.
    Great thanks

  2. Beni says:

    Saved the day! Lost almost a week trying to add the credentials in the right way! Just out of curiosity why the integration can’t be made using this method:
    client.ClientCredentials.UserName.UserName = [UserName];
    client.ClientCredentials.UserName.Password = [Password];

    • Ajith Srikukan says:

      I’m glad to know that 🙂
      Yes, this can be made easy by just passing the credentials via “client.ClientCredentials.UserName.” Not sure, what’s their underneath plan.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: