user3668820
user3668820

Reputation: 3

Consuming XML rest service in .Net

I've created and hosted a restful web service using MVC and C# and Web API 2. What I trying to do now is consume the web service using a windows forms application in VB.net.

Can anyone tell me howI might go about using this? I want to be able to GET and PUT.

I've been able to call the service with the following code but my response is

[{"BookingID":1,"BookingReference":"88764GH","CheckinDate":"15/06/2014","CheckoutDate":"17>/06/2014","Room":122,"RoomType":"Double"]

Is there a way to even know if this is even in XML format? apologies if this is a stupid question I'm new to this. I'm assuming its in XML format if it is called using the WEB API 2 controller that displays it in XML online.

    ' Creates an HttpWebRequest for the specified URL.  
    Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create("http://mynewwebservice.com/api/booking/1"), HttpWebRequest)
    ' Sends the request and waits for a response for booking with ID 1.

    Dim myHttpWebResponse As HttpWebResponse = CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
    ' Calls the method GetResponseStream to return the stream associated with the response. 

    Dim receiveStream As Stream = myHttpWebResponse.GetResponseStream()
    Dim encode As Encoding = System.Text.Encoding.GetEncoding("utf-8")
    ' Pipes the response stream to a higher level stream reader with the required encoding format.  

    Dim readStream As New StreamReader(receiveStream, encode)

    ListBox1.Items.Add(ControlChars.Lf + ControlChars.Cr + "Response stream received")
    Dim read(256) As [Char]
    ' Reads 256 characters at a time.     

    Dim count As Integer = readStream.Read(read, 0, 256)
    ListBox1.Items.Add("HTML..." + ControlChars.Lf + ControlChars.Cr)
    ListBox1.Items.Add("BREAK 1")
    While count > 0
        ' Dumps the 256 characters to a string and displays the string to the console.

        Dim str As New [String](read, 0, count)

        ListBox1.Items.Add(str)

        count = readStream.Read(read, 0, 256)
        ' xmlDocument.LoadXml(String.Format("<root>{0}</root>", readStream.ReadToEnd))

    End While

    ListBox1.Items.Add("")
    ' Releases the resources of the Stream.
    readStream.Close()
    ' Releases the resources of the response.
    myHttpWebResponse.Close()

I essentially have it but I can't extract the required nodes? I want to extract the room number, update it and then send it back to the web service?

The xml if viewed online is as follows:

                          <ArrayofBooking>
                               <Booking>
                                   <BookingID>1</BookingID>
                                   <BookingRef>88764GH</BookingRef>
                                   <CheckinDate>15/06/2014</CheckinDate>
                                   <CheckoutDate>17/06/2014</CheckoutDate>
                                   <Room>122</Room>
                                   <RoomType>Double</RoomType>
                                </Booking>
                            </ArrayofBooking>

Any help will be VERY much appreciated!!

Upvotes: 0

Views: 1464

Answers (1)

Cam Bruce
Cam Bruce

Reputation: 5689

That is JSON. The server will return JSON or XML depending on what the browser says it can ACCEPT.

Please see the ASP.NET Web API Content Negotiation for more info on how to consume whatever format you want your data in.

Basically, you will want to add an ACCEPT header with a application/xml content type to your HttpRequest object

Upvotes: 1

Related Questions