Introduction
Creating and using web service in Visual studio is very simple. Using the Add Web References, one can easy call a web service and make use of the methods. Using this approach, Visual Studio normally generate what we call a Proxy Class. To call the web service, simply use the Proxy Class as you would for any other component. Example
{codecitation class="brush: csharp; gutter: true;" width="650px"}
using (WebRef.Service wService = new WebRef.Service()) { Console.WriteLine(wService.HelloWord()); }
{/codecitation} The purpose of this article is not to show the Proxy class but to demonstrate how to use the HttpWebRequest and HttpWebResponse to call a web service without using the Proxy Class. In order to use HttpWebResponse and HttpWebRequest classes, you have to modify the Web Service configuration file to enable the HTTP GET and HTTP POST Protocols. To enable the HTTP GET and HTTP POST add the following to you web.config file under the
HttpGet enable the HTTP GET Protocol HttpPost enables the HTTP POST Protocol and AnyHttpSoap enables any version of the HTTP SOAP Protocol.
To call the web service using the HTTP GET, the function below demonstrate how to call the Web Service and pass the necessary parameters alongside. Suppose we have a Web Service with the URL as http://127.0.0.1:8080/Service.asmx and the method SendRequest which require the following parameters
Transaction - Int64 Phonenumber - String Amount - int Flag - Boolean
{codecitation class="brush: csharp; gutter: true;" width="650px"} private static void ProcessGetHttpRequest() { Uri myUri = new Uri("http://127.0.0.1:8080/Service.asmx/SendRequest?TransactionID=11&PhoneNumber=4444444&Amount=454&Flag=true");
HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(myUri); HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); Console.WriteLine(); if (HttpWResp.StatusCode ==HttpStatusCode.OK) Console.WriteLine("\nRequest succeeded and the requested information is in the response ,Description : {0}", HttpWResp.StatusDescription); if (myUri.Equals(HttpWResp.ResponseUri)) Console.WriteLine("\nThe Request Uri was not redirected by the server"); else Console.WriteLine("\nThe Request Uri was redirected to :{0}", HttpWResp.ResponseUri); Console.WriteLine("Content length is {0}", HttpWResp.ContentLength); Console.WriteLine("Content type is {0}", HttpWResp.ContentType); Console.WriteLine("Protocol Version is {0}", HttpWResp.ProtocolVersion.ToString());
// Get the stream associated with the response.
Stream receiveStream = HttpWResp.GetResponseStream(); // Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); Console.WriteLine(); Console.WriteLine(readStream.ReadToEnd()); //Insert your code there HttpWResp.Close(); readStream.Close(); }
{/codecitation} When using the HTTP GET, you pass all the parameters through the url. Because using this approach is not Type Safe, you have to be careful with the data type. The format for calling the web service method is /?Parameter1= value>&Parameter2=.
Using our example
is http://127.0.0.1:8080/Service.asmx is SendRequest
The above function simply call the web service and pass the necessary values. This is done using the HttpWebRequest and HttpWebResponse classes. You can get more information about these classes from MSDN. Using the GetResponseStream() function we were able to retrieve the response from the Web Service method and display the response on the Console.
Using the HTTP POST protocol is similar to the GET Protocol. The function below demonstrate how to call the Web Service using the HTTP POST Protocol by passing the necessary parameters and reading the responses.
{codecitation class="brush: csharp; gutter: true;" width="650px"} private static void ProcessPostHttpRequest() { Uri myUri = new Uri("http://192.168.0.11:8080/Service.asmx/SendRequest "); HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(myUri); HttpWReq.Method = "POST"; string postData = "TransactionID=11&Phonenumber=98454111091&Amount=40900&Flag=false"; ASCIIEncoding encoding = new ASCIIEncoding(); byte[] byte1 = encoding.GetBytes(postData);
//HttpWReq.ProtocolVersion // Set the content type of the data being posted. HttpWReq.ContentType = "application/x-www-form-urlencoded";
// Set the content length of the string being posted. HttpWReq.ContentLength = byte1.Length; Stream newStream = HttpWReq.GetRequestStream();
newStream.Write(byte1, 0, byte1.Length); Console.WriteLine("The value of 'ContentLength' property after sending the data is {0}", HttpWReq.ContentLength);
// Close the Stream object. newStream.Close();
HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse(); Console.WriteLine(); Console.WriteLine("Content length is {0}", HttpWResp.ContentLength); Console.WriteLine("Content type is {0}", HttpWResp.ContentType); Console.WriteLine("Protocol Version is {0}", HttpWResp.ProtocolVersion.ToString());
// Get the stream associated with the response.
Stream receiveStream = HttpWResp.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); Console.WriteLine(); Console.WriteLine(readStream.ReadToEnd()); //insert your code here HttpWResp.Close(); readStream.Close(); }
{/codecitation}
Thank you
|