本文整理汇总了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();
}
示例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;
}
示例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;
}
示例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();
}