C# 在.Net中使用XML rest服务
我使用MVC、C#和WebAPI2创建并托管了一个RESTfulWeb服务。我现在要做的是使用VB.net中的windows窗体应用程序使用web服务 谁能告诉我怎么用这个吗?我希望能够得到和投入 我已经能够使用以下代码调用服务,但我的响应是 [{“BookingID”:1,“BookingReference”:“88764GH”,“CheckinDate”:“2014年6月15日”,“CheckoutDate”:“2014年6月17日”,“Room”:122,“RoomType”:“Double”] 有没有办法知道它是否是XML格式的?如果这是一个愚蠢的问题,我深表歉意。如果它是使用WEB API 2控制器调用的,则我假设它是XML格式的,该控制器以XML在线显示它C# 在.Net中使用XML rest服务,c#,.net,xml,vb.net,rest,C#,.net,Xml,Vb.net,Rest,我使用MVC、C#和WebAPI2创建并托管了一个RESTfulWeb服务。我现在要做的是使用VB.net中的windows窗体应用程序使用web服务 谁能告诉我怎么用这个吗?我希望能够得到和投入 我已经能够使用以下代码调用服务,但我的响应是 [{“BookingID”:1,“BookingReference”:“88764GH”,“CheckinDate”:“2014年6月15日”,“CheckoutDate”:“2014年6月17日”,“Room”:122,“RoomType”:“Doubl
' 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()
”为指定的URL创建HttpWebRequest。
将myHttpWebRequest设置为HttpWebRequest=CType(WebRequest.Create(“http://mynewwebservice.com/api/booking/1),HttpWebRequest)
'发送请求并等待ID为1的预订响应。
将myHttpWebResponse设置为HttpWebResponse=CType(myHttpWebRequest.GetResponse(),HttpWebResponse)
'调用方法GetResponseStream以返回与响应关联的流。
Dim receiveStream As Stream=myHttpWebResponse.GetResponseStream()
Dim encode As Encoding=System.Text.Encoding.GetEncoding(“utf-8”)
'使用所需的编码格式将响应流传送到更高级别的流读取器。
Dim readStream作为新的StreamReader(接收流,编码)
ListBox1.Items.Add(ControlChars.Lf+ControlChars.Cr+“收到的响应流”)
Dim读取(256)为[字符]
'一次读取256个字符。
作为整数的Dim计数=readStream.Read(Read,0,256)
ListBox1.Items.Add(“HTML…”+ControlChars.Lf+ControlChars.Cr)
列表框1.Items.Add(“断开1”)
当计数>0时
'将256个字符转储为字符串,并将该字符串显示到控制台。
Dim str为新[字符串](读取,0,计数)
列表框1.Items.Add(str)
count=readStream.Read(Read,0,256)
'xmlDocument.LoadXml(String.Format(“{0}”,readStream.ReadToEnd))
结束时
ListBox1.Items.Add(“”)
'释放流的资源。
readStream.Close()
'释放响应的资源。
myHttpWebResponse.Close()
我基本上拥有它,但我无法提取所需的节点?我想提取房间号,更新它,然后将其发送回web服务
如果在线查看,则xml如下所示:
1.
88764GH
15/06/2014
17/06/2014
122
双重的
非常感谢您的帮助!!这是JSON。服务器将返回JSON或XML,具体取决于浏览器所说的它可以接受的内容 有关如何使用数据格式的更多信息,请参阅
基本上,你会想在你的
HttpRequest
对象中添加一个ACCEPT
标题和一个application/xml
内容类型。非常感谢,现在我甚至都不知道它是JSON!我还可以使用JSON并提取值吗?如果可以,我可以通过编辑JSON对象发回吗?或者xml是ea吗sier?我对这一切都很陌生,所以无论什么更容易开始学习的方法都是我的首选方法。
<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>