Inside the WSDL & Web Services Proxies
page 14 of 16
by Anonymous Contributor
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 65486/ 118

Web Service Client Rules for Inheritors: Rule #4

Web Service clients using HTTP-POST protocol must set ReturnFormatter to XmlReturnReader and ParameterFormatter to HtmlFormParameterWriter in HttpMethodAttribute attribute.

In our case it would look like below:

[System.Web.Services.Protocols.HttpMethodAttribute(typeof(System.Web.Services.Protocols.XmlReturnReader),

typeof(System.Web.Services.Protocols.HtmlFormParameterWriter))]

With the above rules in place we can now hand-code “proxy” code in code-behind file to invoke the Web Service to get the Flight Status.

I have defined the following three classes to implement each of the above-mentioned protocol.

For SOAP protocol:

//This implements SOAP

[System.Web.Services.WebServiceBindingAttribute(Name="FlightServiceSoap",

Namespace="http://tempuri.org/")]

public class SoapCall : System.Web.Services.Protocols.SoapHttpClientProtocol

{

public SoapCall(string url)

{

this.Url = url;

}

[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/GetFlightStatus",

Use=System.Web.Services.Description.SoapBindingUse.Literal,

ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]

public string GetFlightStatus(int nFlightNo, int nDayOffset)

{

object[] result = this.Invoke("GetFlightStatus", new object []

{nFlightNo, nDayOffset});

return (string)(result[0]);

}

}

 

For HTTP-GET:

 

//This implements HTTP-GET

public class HttpGetCall : System.Web.Services.Protocols.HttpGetClientProtocol

{

public HttpGetCall(string url)

{

this.Url = url;

}

[System.Web.Services.Protocols.HttpMethodAttribute(typeof(System.Web.Services.Protocols.XmlReturnReader),

typeof(System.Web.Services.Protocols.UrlParameterWriter))]

[return: System.Xml.Serialization.XmlRootAttribute("string",

Namespace="http://tempuri.org/", IsNullable=true)]

public string GetFlightStatus(string nFlightNo, string nDayOffset)

{

object result = this.Invoke("GetFlightStatus", this.Url +

"/GetFlightStatus", new object [] {nFlightNo, nDayOffset});

return result.ToString();

}

}

 

For HTTP-POST protocol:

 

//This implements HTTP-POST

public class HttpPostCall :

System.Web.Services.Protocols.HttpPostClientProtocol

{

public HttpPostCall(string url)

{

this.Url = url;

}

[System.Web.Services.Protocols.HttpMethodAttribute(typeof(System.Web.Services.Protocols.XmlReturnReader),

typeof(System.Web.Services.Protocols.HtmlFormParameterWriter))]

[return: System.Xml.Serialization.XmlRootAttribute("string",

Namespace="http://tempuri.org/", IsNullable=true)]

public string GetFlightStatus(string nFlightNo, string nDayOffset)

{

return (string)this.Invoke("GetFlightStatus", this.Url +

"/GetFlightStatus", new object [] {nFlightNo, nDayOffset});

}

}

In all the cases mentioned above, you could find one similarity: the Invoke method. This call will invoke the actual web service using corresponding protocol. Also as we can see the Invoke method takes the Web Service Method name and an array of objects containing parameters to pass to the remote Web Service. One important thing to keep in mind is that the order of the values in the array must correspond to the order of the parameters in the calling method. 

And also we have to make sure the following namespaces are defined in the “code behind” file:

using System.Web.Services.Protocols;

using System.Xml.Serialization;

using System.Web.Services; 

Now, we are all set to consume the Web Service. The code snippet below shows how to invoke the Web Service using the SOAP protocol:

SoapCall objSP = new

SoapCall("http://webservices.scandinavian.net/flightstatus/flightservice.asmx")

;

flightInfo.Text = objSP.GetFlightStatus(int.Parse(flightNo.Text),

int.Parse(offset.SelectedItem.Value)); 

(For detailed code please take a look at the support material)

I did not add any processing to parse the XML to show the results since it is out of the scope of this article. But basically we are ready to communicate with the Scandinavian Web Service for SAS Flight Status. You can also verify the results using the SAS Flight Status page.


View Entire Article

User Comments

Title: Webservice   
Name: Anbazhagan. P
Date: 2009-03-14 12:47:38 PM
Comment:
Very useful website
Title: rakesh   
Name: Name rakesh
Date: 2007-09-13 3:38:58 AM
Comment:
Ok Run
Title: Getting Error   
Name: Dilip Kumar Prusty
Date: 2006-09-18 5:19:48 AM
Comment:
I am getting
The request failed with HTTP status 404: Not Found.
this error .

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-03-29 9:28:51 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search