如何使用HttpClient读取XML响应? [英] How to use HttpClient to read an XML response?
问题描述
显然,HttpClient
是提出HTTP请求的新推荐方式,因此我试图用它向Delicious API发出请求,该API返回XML响应.这就是我所拥有的:
Apparently HttpClient
is the new recommended way of making HTTP requests, so I'm trying to use it to make a request to the Delicious API, which returns back an XML response. Here's what I've got:
internal class Program
{
private static void Main(string[] args)
{
var credentials = new NetworkCredential("username", "password");
var handler = new HttpClientHandler { Credentials = credentials};
var client = new HttpClient(handler);
var suggest = new Uri("https://api.del.icio.us/v1/posts/suggest");
var suggestions =
client.GetAsync(suggest.AddQueryParams("url", "https://yahoo.com"))
.ContinueWith(t => t.Result.Content.ReadAsAsync<DeliciousSuggest>())
.Unwrap()
.Result;
Console.ReadLine();
}
}
public class DeliciousSuggest
{
public string[] Popular { get; set; }
public string[] Recommended { get; set; }
public string[] Network { get; set; }
}
但是,它会在ReadAsAsync
位上引发异常,
However, it throws an exception on the ReadAsAsync
bit,
其他信息:无法加载文件或程序集"Newtonsoft.Json,版本= 4.5.0.0,文化=中性,PublicKeyToken = 30ad4fe6b2a6aeed"或其依赖项之一.系统找不到指定的文件.
Additional information: Could not load file or assembly 'Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed' or one of its dependencies. The system cannot find the file specified.
也许我错过了一些集会,但这使我感到震惊.返回类型是XML,而不是JSON,但是我仍然不确定此ReadAsAsync
方法如何工作,甚至我将如何指定它.
Perhaps I'm missing some assembly, but that strikes me as off. The return type is XML, not JSON, but I'm still not quite sure how this ReadAsAsync
method works, or how I would even specify that.
示例响应如下:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<suggest>
<popular>yahoo!</popular>
<popular>yahoo</popular>
<popular>web</popular>
<popular>tools</popular>
<popular>searchengines</popular>
<recommended>yahoo!</recommended>
<recommended>yahoo</recommended>
<recommended>web</recommended>
<network>for:Bernard</network>
<network>for:britta</network>
<network>for:deusx</network>
</suggest>
如何将其解析为可用格式?
How can I parse that into some usable format?
推荐答案
如何将其解析为可用格式?"
"How can I parse that into some usable format?"
[XmlRoot("suggest")]
public class DeliciousSuggest {
[XmlElement("popular")]
public string[] Popular { get; set; }
[XmlElement("recommended")]
public string[] Recommended { get; set; }
[XmlElement("network")]
public string[] Network { get; set; }
}
并使用XmlSerializer反序列化.
and use XmlSerializer to deserialize.
您应该以字符串形式从del.icio.us读回响应,然后可以按以下方式反序列化它:
You should read the response back from del.icio.us as a string, and then you can deserialize it as follows:
var s = "this is the response from del";
var buffer = Encoding.UTF8.GetBytes(s);
using (var stream = new MemoryStream(buffer)) {
var serializer = new XmlSerializer(typeof(DeliciousSuggest));
var deliciousSuggest = (DeliciousSuggest)serializer.Deserialize(stream);
//then do whatever you want
}
这篇关于如何使用HttpClient读取XML响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!