【发布时间】:2014-06-04 16:48:00
【问题描述】:
我已经使用 MVC 和 C# 以及 Web API 2 创建并托管了一个安静的 Web 服务。我现在要做的是使用 VB.net 中的 Windows 窗体应用程序来使用该 Web 服务。
谁能告诉我如何使用它?我希望能够 GET 和 PUT。
我已经能够使用以下代码调用该服务,但我的响应是
[{"BookingID":1,"BookingReference":"88764GH","CheckinDate":"15/06/2014","CheckoutDate":"17>/06/2014","Room":122, "房间类型":"双人房"]
有没有办法知道这是否是 XML 格式?抱歉,如果这是一个愚蠢的问题,我是新手。如果使用 WEB API 2 控制器调用它,我假设它是 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>
非常感谢任何帮助!
【问题讨论】: