Addi Khan
Addi Khan

Reputation: 77

How to save response in my variable from webservice

I am new to web services, I have trial version of flight state, how to get response in C#?

I have below web service

https://api.flightstats.com/flex/airlines/soap/v1/airlinesService?wsdl
https://api.flightstats.com/flex/schedules/soap/v1/scheduledFlightsService?wsdl

I want call its methods and response save in my variable but its shows error ;( I have added this service through Service Reference and call its methods.

This is my code - please help

private void button1_Click(object sender, EventArgs e)
{
    ServiceReference2.flightServiceClient obje = new Windowsflightstate.ServiceReference2.flightServiceClient();

    // How to get response 
    string[] resultdata = obje.flightStatus_arr("b2113423", "65fa387418310aec95a688737f8e0b13", "", "EK4158", 2016, 03, 07, "", "", "", "");

    flightscheduleswebservice.scheduledFlightServiceClient client = new Windowsflightstate.flightscheduleswebservice.scheduledFlightServiceClient();
    client.byFlight_Arriving("b2113423", "65fa387418310aec95a688737f8e0b13",null, "EK4158", 2016, 03, 07, null, null);

    // it is showing error how to get response
    string[] resultdata2 =  client.byFlight_Arriving("b2113423", "65fa387418310aec95a688737f8e0b13", null, "EK4158", 2016, 03, 07, null, null);
}

Please see this image:

enter image description here

Upvotes: 1

Views: 1944

Answers (1)

marc_s
marc_s

Reputation: 755321

Just use this code - assign the response from the web service call to the datatype (class) that the web service method returns:

private void button1_Click(object sender, EventArgs e)
{
    ServiceReference2.flightServiceClient obje = new Windowsflightstate.ServiceReference2.flightServiceClient();

    // How to get response 
    flightscheduleswebservice.responseByFlight resultdata = obje.flightStatus_arr("b2113423", "65fa387418310aec95a688737f8e0b13", "", "EK4158", 2016, 03, 07, "", "", "", "");

    // show errors if any
    Console.WriteLine("HTTP status code: {0}", resultdata.error.httpStatusCode);
    Console.WriteLine("Error code: {0}", resultdata.error.errorCode);
    Console.WriteLine("Error message: {0}", resultdata.error.errorMessage);

    // display info, if any
    if (resultdata.scheduledFlights != null && resultdata.scheduledFlights.Length > 0)
    {
        Console.WriteLine("Number of flights returned: {0}", resultdata.scheduledFlights.Length);
        Console.WriteLine("Flight #1 - carrier name: {0}", resultdata.scheduledFlights[0].carrier.name);
        Console.WriteLine("Flight #1 - departure airport name: {0}", resultdata.scheduledFlights[0].departureAirport.name);
        Console.WriteLine("Flight #1 - arrival airport name: {0}", resultdata.scheduledFlights[0].arrivalAirport.name);
    }
}

From the error it is obvious that the web service calls returns an instance of a class called flightscheduleswebservice.responseByFlight - not an array of strings that you were trying to assign it to.

The whole point of using web services using the built-in mechanisms in C# is that you get back a nice response object - not just a pile of XML strings. You don't need to parse the XML response yourself - that ServiceReference2.flightServiceClient class handles of all this for you - you get back a nice, normal .NET object that you can work with!

Upvotes: 2

Related Questions