Introduction

In this document, I want to explain how to call any web service from any programming language C, C++, C#, Java … and so on.

Using this article, you can also call the web services using handheld or mobile or any terminal that supports TCP/IP protocol.

As we know, we can call web service in C# adding web reference to a project and call the functions of the web service.

If you want to know how, you can Google it and you will find a lot of pages that describe how to do this.

For programmers that need to connect to a web service using C language, they can use any of the SOAP libraries to call the web service. For me, I used GSOAP library. It is good enough to be used in this method.

But here, we do not want to add web reference and we do not want to use any external library.

We want to call the web service using three things, server IP, server PORT, data that has our request and also receive data for response.

2- Server PORT

Now, which port will I choose to send my request to? Actually, you have only one choice here.

Because my call will be as HTTP POST as we will see, we will use the default HTTP port for this to call any web service. This port is 80.

3- Data

The final thing we want to determine to finish our method is the data I will send. Fortunately, it is in our hands. Open my web service example http://www.webservicex.net/globalweather.asmx and choose GetCitiesByCountry function.

but replacing the string with all the returned responses from the web service depending on the function we have called.

Now from any terminal that can send and receive data using TCP/IP protocol, you have IP, Port, data. Send the data to the IP and Port and you can receive the response.

The Code

Here I used C# to make a simple test for my words, I used it because it is very easy for understanding but you can use any other language.

At first, we use System.Net and System.Net.Sockets to can use TCP functions.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;

I used System.IO because I will save my request and response on files so I want read and write operations on files.

I created a TCP client and connect it to the server IP and port 80 as I said before:

TcpClient myclient = new TcpClient();//create a client will not be
//myclient.Connect("173.201.44.188", 80);

My request is on a txt file called data.txt. I opened it and filled the request from it to array of bytes. You can fill this array by the request using any method you want. Only fill it with the request.

I've bumped into a similar problem due to encoding before, but that was because the HTTP request method (GET) was sent AFTER a UTF-8 BOM, which created really cryptic error messages. Yet, it doesn't appear to be the problem in your case, otherwise the HTTP/1.1 400 Bad Request would have come BEFORE the correct response.

One of the was to check this is to open up the request file using a hex editor, and do a byte count to ensure that you've calculated correctly in the first place.