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


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

本文整理汇总了C#中System.Net.WebClient.DownloadStringTaskAsync方法的典型用法代码示例。如果您正苦于以下问题:C# System.Net.WebClient.DownloadStringTaskAsync方法的具体用法?C# System.Net.WebClient.DownloadStringTaskAsync怎么用?C# System.Net.WebClient.DownloadStringTaskAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.Net.WebClient的用法示例。


在下文中一共展示了System.Net.WebClient.DownloadStringTaskAsync方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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

示例2: DownloadStringAsync

		public async Task<string> DownloadStringAsync() {
			if (string.IsNullOrEmpty( RequestUrl )) {
				throw new ArgumentNullException( "reuqest url" );
			}

			var result = string.Empty;

			try {
				using (System.Net.WebClient client = new System.Net.WebClient()) {
					result = await client.DownloadStringTaskAsync( new Uri( RequestUrl ) );
				}
			} catch (Exception e) {
				_logger.Error( e.Message );
				throw;
			}

			return result;
		}
开发者ID:Danielovich,项目名称:facebookpagescraper,代码行数:18,代码来源:UrlRequester.cs

示例3: IsStreamOnlineAsync

        protected override async Task<bool> IsStreamOnlineAsync()
        {
            try
            {
                using (System.Net.WebClient wc = new System.Net.WebClient())
                {
                    string RequestString;
                    RequestString = await wc.DownloadStringTaskAsync(new Uri("https://api.twitch.tv/kraken/streams/" + Username)).ConfigureAwait(false);

                    if (!RequestString.Contains("\"stream\":null"))
                    {
                        return true;
                    }
                }
            }
            catch (Exception)
            {
#if DEBUG
                // Outside of debug mode, we're willing to completely ignore stream retrieval failures.
                throw;
#endif
            }

            return false;
        }
开发者ID:akoltz,项目名称:ggcbnet,代码行数:25,代码来源:Stream.cs

示例4: BTNOpenJsonFromURL_Click

        private void BTNOpenJsonFromURL_Click(object sender, RoutedEventArgs e)
        {
            WindowWithStringResult w = new WindowWithStringResult(async (path) =>
            {
                var result = string.Empty;
                using (var webClient = new System.Net.WebClient())
                {
                    try
                    {
                        result = await webClient.DownloadStringTaskAsync(new Uri(path));
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("Nepodarilo sa stiahnut json");
                        return;
                    }

                    if (!string.IsNullOrEmpty(result))
                    {
                        jsonBase = await Task.Factory.StartNew(() => 
                            {
                                JsonBase b;
                                try
                                {
                                    b = JsonConvert.DeserializeObject<JsonBase>(result, settings);
                                }
                                catch(Exception)
                                {
                                    MessageBox.Show("Neplatny json");
                                    return null;
                                }
                                return b;
                            });

                        TXTSkyboxpath.Text = jsonBase.SkyboxPath;
                        ButtonSaveFile.IsEnabled = true;
                        ButtonManageDW.IsEnabled = true;
                        ButtonAddInterior.IsEnabled = true;
                        ButtonManageManufacturers.IsEnabled = true;
                        ButtonManageRooms.IsEnabled = true;
                        BTNEditProducts.IsEnabled = true;
                    }
                }
            });
            w.ShowDialog();
        }
开发者ID:Lasto13,项目名称:jsonEditor,代码行数:46,代码来源:MainWindow.xaml.cs


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