简体   繁体   中英

Sending GET requests from a .NET client to a non-WCF, plain XML web service

I trying to write a C# client for a web service API ( documentation here , PDF warning). Each request to the API is an HTTP GET call, with parameters encoded in the URI. The response is a well-formed XML document with a Content-Type of “text/xml”. Every request must include my API key as a parameter.

I could implement a web service proxy myself, but my intuition is that there is a built in .NET library written by people who actually get paid to think about this stuff. From all my searching and reading, I think I want to use WCF, but I can't find documentation how how to use WCF as the client , making calls to a plain old XML web service . Is there anything in .NET or Visual Studio that can help me out? I don't use svcutil.exe, do I? Because the web service I'm talking to doesn't have any metadata.

Take a look at the new HttpClient class that is part of the ASP.NET Web Api. I've used it successfully to talk to the stackexchange API and it's very easy to use.

You can use HttpWebRequest to call the web service API, XML library to convert the response string to .net Objects. Here is a simple example (it gets an error message "Invalid API access key supplied" as I don't have a valid key):

    static void Main(string[] args)
    {
        HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(
            @"http://www.ctabustracker.com/bustime/api/v1/gettime?key=89dj2he89d8j3j3ksjhdue93j"
            );

        using (WebResponse resp = req.GetResponse())
        {
            using (Stream respStream = resp.GetResponseStream())
            {
                using(StreamReader reader = new StreamReader(respStream))
                {
                    String respString = reader.ReadToEnd();

                    Debug.WriteLine(respString);
                    TestBusTimeResponse response = XmlUtil.DeserializeString<TestBusTimeResponse>(respString);
                    Debug.WriteLine(response.Error.Message);
                }
            }
        }

        Console.ReadLine();
    }

where XmlUtil.DeserializeString is defined as:

    public static T DeserializeString<T>(String content)
    {
        using (TextReader reader = new StringReader(content))
        {
            XmlSerializer s = new XmlSerializer(typeof(T));
            return (T)s.Deserialize(reader);
        }
    }

and TestBusTimeResponse is defined as (You can actually generate this Business Object class with the XML schema, specified in the API document, using the xsd utility shipped with VS):

[XmlRoot("error")]
public class TestBusTimeResponseError
{
    [XmlElement("msg")]
    public String Message
    {
        get;
        set;
    }
}
// Response in the following format:
// <?xml version="1.0"?>
// <bustime-response><error><msg>Invalid API access key supplied</msg></error></bustime-response>
[XmlRoot("bustime-response")]
public class TestBusTimeResponse
{
    [XmlElement("error")]
    public TestBusTimeResponseError Error
    {
        get;
        set;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM