当前位置: 首页>>代码示例>>C#>>正文


C# System.Net.WebClient.OpenReadTaskAsync方法代码示例

本文整理汇总了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();
        }
开发者ID:chovancova,项目名称:project_summer2015,代码行数:60,代码来源:Program.cs


注:本文中的System.Net.WebClient.OpenReadTaskAsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。