[英]How to download XML with RestClient?
I have a url that contains valid xml, but unsure on how I could retrieve this with RestClient. 我有一个包含有效xml的网址,但不确定如何使用RestClient检索它。 I thought I could just download the string and then parse it like I allready to with WebClient.
我以为我可以下载字符串,然后像使用WebClient一样解析它。
Doing: 正在做:
public static Task<String> GetLatestForecast(string url)
{
var client = new RestClient(url);
var request = new RestRequest();
return client.ExecuteTask<String>(request);
}
Makes VS cry about that 'string' must be a non-abstract type with a public parameterless constructor. 让VS哭泣的是“字符串”必须是具有公共无参数构造函数的非抽象类型。
See executetask: 请参见executetask:
namespace RestSharp
{
public static class RestSharpEx
{
public static Task<T> ExecuteTask<T>(this RestClient client, RestRequest request)
where T : new()
{
var tcs = new TaskCompletionSource<T>(TaskCreationOptions.AttachedToParent);
client.ExecuteAsync<T>(request, (handle, response) =>
{
if (response.Data != null)
tcs.TrySetResult(response.Data);
else
tcs.TrySetException(response.ErrorException);
});
return tcs.Task;
}
}
}
Thanks to Claus Jørgensen btw for a awesome tutorial on Live Tiles! 多亏了ClausJørgensenbtw提供了有关Live Tiles的出色教程!
I just want to download the string as I already have a parser waiting for it to parse it :-) 我只想下载该字符串,因为我已经有一个解析器等待它解析:-)
If all you want is a string, just use this approach instead: 如果您只需要一个字符串,请改用这种方法:
namespace RestSharp
{
public static class RestSharpEx
{
public static Task<string> ExecuteTask(this RestClient client, RestRequest request)
{
var tcs = new TaskCompletionSource<string>(TaskCreationOptions.AttachedToParent);
client.ExecuteAsync(request, response =>
{
if (response.ErrorException != null)
tcs.TrySetException(response.ErrorException);
else
tcs.TrySetResult(response.Content);
});
return tcs.Task;
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.