【问题标题】:Consuming XML rest service in .Net在 .Net 中使用 XML 休息服务
【发布时间】: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>

非常感谢任何帮助!

【问题讨论】:

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


    【解决方案1】:

    那是 JSON。服务器将根据浏览器所说的返回 JSON 或 XML ACCEPT

    请参阅ASP.NET Web API Content Negotiation,了解有关如何使用您想要的数据格式的更多信息。

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

    【讨论】:

    • 非常感谢,现在我什至不知道它是 JSON 感到愚蠢!我仍然可以使用 JSON 并提取值吗?如果是这样,我可以通过编辑 JSOn 对象回发吗?或者 XML 会更容易吗?我对这一切都很陌生,所以我更喜欢开始学习的方法。
    猜你喜欢
    • 2010-12-09
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-09
    相关资源
    最近更新 更多