本文整理汇总了C#中System.Net.WebClient.OpenReadTaskAsync方法的典型用法代码示例。如果您正苦于以下问题:C# System.Net.WebClient.OpenReadTaskAsync方法的具体用法?C# System.Net.WebClient.OpenReadTaskAsync怎么用?C# System.Net.WebClient.OpenReadTaskAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Net.WebClient
的用法示例。
在下文中一共展示了System.Net.WebClient.OpenReadTaskAsync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
// System.Web.Extensions.dll
// http://localhost:1234/MyService.svc/json/4
// http://localhost:1234/MyService.svc/xml/4
static void Main(string[] args)
{
Action action = async () =>
{
var stopwatch = new System.Diagnostics.Stopwatch();
Console.WriteLine("JSON (JavaScriptSerializer)");
{
// fetch data (as JSON string)
var url = new Uri("http://localhost:1234/MyService.svc/json/4");
var client = new System.Net.WebClient();
var json = await client.DownloadStringTaskAsync(url);
// deserialize JSON into objects
var serializer = new JavaScriptSerializer();
var data = serializer.Deserialize<JSONSAMPLE.Data>(json);
// use the objects
Console.WriteLine(data.Number);
foreach (var item in data.Multiples)
Console.Write("{0}, ", item);
}
Console.WriteLine();
Console.WriteLine("JSON (DataContractJsonSerializer)");
{
var url = new Uri("http://localhost:1234/MyService.svc/json/4");
var client = new System.Net.WebClient();
var json = await client.OpenReadTaskAsync(url);
var serializer = new DataContractJsonSerializer(typeof(DATACONTRACT.Data));
var data = serializer.ReadObject(json) as DATACONTRACT.Data;
Console.WriteLine(data.Number);
foreach (var item in data.Multiples)
Console.Write("{0}, ", item);
}
Console.WriteLine();
Console.WriteLine("XML");
{
var url = new Uri("http://localhost:1234/MyService.svc/xml/4");
var client = new System.Net.WebClient();
var xml = await client.DownloadStringTaskAsync(url);
var bytes = Encoding.UTF8.GetBytes(xml);
using (MemoryStream stream = new MemoryStream(bytes))
{
var serializer = new XmlSerializer(typeof(XMLSAMPLE.Data));
var data = serializer.Deserialize(stream) as XMLSAMPLE.Data;
Console.WriteLine(data.Number);
foreach (var item in data.Multiples)
Console.Write("{0}, ", item);
}
}
};
action.Invoke();
Console.Read();
}