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.