locked
get XML data from WCF service RRS feed

  • Question

  • User842972490 posted

    Good morning,

    I have a third party wcf service, below is their API url for the service

    WSDL Location - https://api.cars.com/en/soap/index?wsdl
    Server End Point - https://api.cars.com/en/soap/index

    and i am trying to consume a method from their service, getVehicles. It gets an xml response from me, the client and returns an xml back.

    below is the code i use to get the data, but can seem to find how to invoke the service.

    try
                {
                    string oRequest = @"My XML data to be sent.";
    
                    //Builds the connection to the WebService.
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://api.cars.com/en/soap/index?wsdl");
                    req.Headers.Add("SOAPAction", @"http://api.cars.com/en/soap/index/getVehicles");
                    req.ContentType = "text/xml; charset=\"utf-8\"";
                    req.Accept = "text/xml";
                    req.Method = "POST";
    
                    //Passes the SoapRequest String to the WebService
                    using (Stream stm = req.GetRequestStream())
                    {
                        using (StreamWriter stmw = new StreamWriter(stm))
                        {
                            stmw.Write(oRequest);
                        }
                    }
                    //Gets the response
                    WebResponse response = req.GetResponse();
                    //Writes the Response
                    Stream responseStream = response.GetResponseStream();
    
                    StreamReader sr = new StreamReader(responseStream);
                    string s = sr.ReadToEnd();
    
                    XmlDocument doc = new XmlDocument();
    
    
                    return s;
                }
                catch (Exception e)
                {
                    return e.ToString();
    
                    if (e is WebException && ((WebException)e).Status == WebExceptionStatus.ProtocolError)
                    {
                        WebResponse errResp = ((WebException)e).Response;
                        using (Stream respStream = errResp.GetResponseStream())
                        {
                            //return respStream.ToString();
                        }
                    }
    
                    return string.Empty;
                }


    i get an internal error (500) from web server, and if i remove the /getVehicles from SOAPAction i get a xml of the service metadata i think. Any ideas what i am doing wrong?? Thanks for the help in advance...

    One more thing the URI's are fictional but are following the real structure...

    Monday, July 22, 2013 7:39 AM

Answers

  • User220959680 posted

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://api.cars.com/en/soap/index?wsdl");
                    req.Headers.Add("SOAPAction", @"http://api.cars.com/en/soap/index/getVehicles");

    Note that service URI is Https , NOT http 

    Refer http://www.codeproject.com/Articles/328552/Calling-a-WCF-service-from-a-client-without-having

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 22, 2013 11:41 AM
  • User220959680 posted

    Made the changes but when i call the web service i just get a detailed XML file with the available services they provide. Its like i am not sending the XML file they need so i can get results.

    req.Headers.Add("SOAPAction", @"http://api.cars.com/en/soap/index/getVehicles");
    req.Method = "POST";

    Sounds like the service is returning WSDL (Web services Description Language) file.

    HTTP verb to request data from the service is GET. Http verb POST is used to post data from the client to the service to create a new record in database in general. 

    In the above request client application is posting  string data to the service even it is requesting some data. Contact the service provider.

    As the service is secured (Https) it is required to get authenticated, please check with the service provider the request you should be sending to get the required data.

    Rest of the client source seems error-free.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 27, 2013 5:27 PM

All replies

  • User220959680 posted

    HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://api.cars.com/en/soap/index?wsdl");
                    req.Headers.Add("SOAPAction", @"http://api.cars.com/en/soap/index/getVehicles");

    Note that service URI is Https , NOT http 

    Refer http://www.codeproject.com/Articles/328552/Calling-a-WCF-service-from-a-client-without-having

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 22, 2013 11:41 AM
  • User842972490 posted

    Made the changes but when i call the web service i just get a detailed XML file with the available services they provide. Its like i am not sending the XML file they need so i can get results.

    Tuesday, July 23, 2013 1:17 AM
  • User220959680 posted

    Made the changes but when i call the web service i just get a detailed XML file with the available services they provide. Its like i am not sending the XML file they need so i can get results.

    req.Headers.Add("SOAPAction", @"http://api.cars.com/en/soap/index/getVehicles");
    req.Method = "POST";

    Sounds like the service is returning WSDL (Web services Description Language) file.

    HTTP verb to request data from the service is GET. Http verb POST is used to post data from the client to the service to create a new record in database in general. 

    In the above request client application is posting  string data to the service even it is requesting some data. Contact the service provider.

    As the service is secured (Https) it is required to get authenticated, please check with the service provider the request you should be sending to get the required data.

    Rest of the client source seems error-free.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, July 27, 2013 5:27 PM