在.Net中使用XML休息服务

时间:2014-06-04 16:48:00

标签: c# .net xml vb.net rest

我使用MVC和C#以及Web API 2创建并托管了一个安静的Web服务。我现在要做的是使用VB.net中的Windows窗体应用程序来使用Web服务。

任何人都可以告诉我如何使用它吗?我希望能够获得和PUT。

我已经能够使用以下代码调用该服务,但我的回复是

  

[{ “BookingID”:1, “BookingReference”: “88764GH”, “CheckinDate”: “15/06/2014”, “CheckoutDate”: “17> / 06/2014”, “室”:122, “RoomType”: “双”]

有没有办法甚至知道这是否是XML格式?道歉,如果这是一个愚蠢的问题,我是新手。如果使用WEB API 2控制器调用XML格式,我假设它采用XML格式。该控制器在线显示XML格式。

    ' 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()

我基本上拥有它,但我无法提取所需的节点?我想提取房间号,更新房间号然后再发回网络服务?

如果在线查看xml如下:

                          <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>

非常感谢任何帮助!!

1 个答案:

答案 0 :(得分:1)

那是JSON。服务器将返回JSON或XML,具体取决于浏览器所说的ACCEPT

有关如何使用您希望数据输入格式的更多信息,请参阅ASP.NET Web API Content Negotiation

基本上,您需要向ACCEPT对象添加带有application/xml内容类型的HttpRequest标头