在.Net中使用XML rest服务

本文关键字:XML rest 服务 Net | 更新日期: 2023-09-27 18:06:37

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

有人能告诉我如何使用这个吗?我希望能够得到和投入。

我已经可以用以下代码呼叫该服务,但我的响应是

[{"BookingID":1,"BookingReference":"88764GH","入住日期":"2014年6月15日","退房日期":

有没有办法知道这是否是XML格式的?如果这是一个愚蠢的问题,我很抱歉。我假设它是XML格式的,如果它是使用WEB API 2控制器调用的,该控制器以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()

我基本上有它,但我不能提取所需的节点?我想提取房间号码,更新它,然后将其发送回web服务?

如果在线查看,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>

任何帮助都将不胜感激!!

在.Net中使用XML rest服务

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

请参阅ASP.NET Web API内容协商,了解有关如何使用所需数据格式的更多信息。

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