本文整理汇总了C#中HttpClient.GetBufferAsync方法的典型用法代码示例。如果您正苦于以下问题:C# HttpClient.GetBufferAsync方法的具体用法?C# HttpClient.GetBufferAsync怎么用?C# HttpClient.GetBufferAsync使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类HttpClient
的用法示例。
在下文中一共展示了HttpClient.GetBufferAsync方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Button_Click
private async void Button_Click(object sender, RoutedEventArgs e)
{
//this.txt.Text = DateTime.Now.ToString();
//string x = "";
//HttpClient h = new HttpClient();
//try
//{
// var response = h.GetBufferAsync(new Uri("http://202.202.43.125/"));
// x = await h.GetStringAsync(new Uri("http://202.202.43.125/"));
//}
//catch (Exception xx)
//{
// Debug.WriteLine(xx.Message);
//}
//Debug.WriteLine(x);
//Debug.WriteLine("-=--------------------------------------------------------");
////异步执行并得到结果
//x = await getstring(new Uri("http://202.202.43.125/"));
////异步赋值在执行
//Task<string> rrrrr = getstring(new Uri("http://202.202.43.125/"));
//var c = await rrrrr;
//Debug.WriteLine(x);
HttpClient httpclient = new HttpClient();
var uri = new Uri("http://apis.baidu.com/songshuxiansheng/real_time/search_news?keyword=明星");
httpclient.DefaultRequestHeaders.Add("apikey", "9a84555d8b243d4afa83cac9855b60e7");
var response = await httpclient.GetAsync(uri);
var r2 = await httpclient.GetStringAsync(uri);
var r3 = await httpclient.GetBufferAsync(uri);
var r33 = r3.ToArray();
string result = Encoding.UTF8.GetString(r33);
string humanlike = Decode(result);
string text = "Aa0严";
var cccccccc = Encoding.Unicode.GetBytes(text);
Debug.Write(humanlike);
//char[] aaaa =
//{
// (Char)0x4ed6,(Char)0x662f,(Char)0x5a31,(Char)0x4e50,(Char)0x5708,(Char)0x91cc,(Char)0x6700,(Char)0x5bd2,(Char)0x9178,(Char)0x7684,(Char)0x660e,(Char)0x661f,(Char)0xff0c,(Char)0x5374,(Char)0x503c,(Char)0x5f97,(Char)0x6bcf,(Char)0x4e00,(Char)0x4e2a,(Char)0x4eba,(Char)0x5c0a,(Char)0x91cd,(Char)0xff01
//};
//foreach (var VARIABLE in aaaa)
//{
// Debug.Write(VARIABLE);
//}
}
示例2: DownloadBufferAsync
public async static System.Threading.Tasks.Task<IBuffer> DownloadBufferAsync(Uri url)
{
//Turn off cache for live playback
var filter = new Windows.Web.Http.Filters.HttpBaseProtocolFilter();
filter.CacheControl.WriteBehavior = Windows.Web.Http.Filters.HttpCacheWriteBehavior.NoCache;
using (var httpClient = new HttpClient(filter))
{
try
{
var buffer = await httpClient.GetBufferAsync(url);
#if DEBUG
Logger.Log("GetBufferAsync suceeded for url: " + url);
#endif
return buffer;
}
catch (Exception e)
{
#if DEBUG
Logger.Log("GetBufferAsync exception for url: " + url + " HRESULT 0x" + e.HResult.ToString("x"));
#endif
return null;
}
}
}
示例3: SaveFile
private async Task SaveFile(StorageFile file)
{
string url = ViewModel.Wallpaper.GetCacheUrl(AppSetting.WallpaperSize);
using (HttpClient client = new HttpClient())
{
await FileIO.WriteBufferAsync(file, await client.GetBufferAsync(new Uri(url)));
}
}
示例4: GetWallpaperFile
/// <summary>
/// 获取当前壁纸文件。该方法用于壁纸设置和锁屏。
/// </summary>
/// <returns></returns>
private async Task<StorageFile> GetWallpaperFile()
{
var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(Guid.NewGuid().ToString());
using (HttpClient client = new HttpClient())
{
string url = ViewModel.Wallpaper.GetCacheUrl(AppSetting.WallpaperSize);
await FileIO.WriteBufferAsync(file, await client.GetBufferAsync(new Uri(url)));
}
return file;
}
示例5: GetImageData
private async Task<byte[]> GetImageData()
{
using (HttpClient client = new HttpClient())
{
return (await client.GetBufferAsync(new Uri(ViewModel.Wallpaper.GetCacheUrl(AppSetting.WallpaperSize)))).ToArray();
}
}
示例6: GetThumbnailDataAsync
private async Task<byte[]> GetThumbnailDataAsync()
{
using (HttpClient client = new HttpClient())
{
return (await client.GetBufferAsync(new Uri(_feed.PicMid))).ToArray();
}
}
示例7: UriSourceChanged
private async void UriSourceChanged()
{
IsLoading = true;
#region 清空之前的图像
using (var emptyStream = new InMemoryRandomAccessStream())
{
SetSource(emptyStream);
}
#endregion 清空之前的图像
// 清空图像。
if (UriSource == null)
{
IsLoading = false;
return;
}
var bitmapImage = UriSource as BitmapImage;
var uri = bitmapImage != null ? bitmapImage.UriSource : null;
if (uri == null)
{
IsLoading = false;
throw new InvalidOperationException("not support image source");
}
var requestTime = DateTime.Now;
_lastRequestTime = requestTime;
var scheme = uri.Scheme;
if (scheme == "http" || scheme == "https")
{
// 网络图像。
byte[] networkImageDatas;
// 获取本地对应的缓存路径。
var cachePath = GetCachePath(uri);
// 本地图片缓存文件是否存在。
var isCacheExist = IsCacheExist(cachePath);
if (isCacheExist == false)
{
// 缓存不存在,下载图像。
using (var client = new HttpClient())
{
try
{
networkImageDatas = (await client.GetBufferAsync(uri)).ToArray();
}
catch (Exception ex)
{
// 下载失败。
if (ImageFailed != null)
{
ImageFailed(this, new ImageFailedEventArgs(ex));
}
IsLoading = false;
return;
}
}
// 下载成功,保存到独立存储。
await SaveImageAsync(cachePath, networkImageDatas);
}
else
{
// 缓存存在,加载独立存储中的缓存图像。
networkImageDatas = await LoadCachedImageAsync(cachePath);
}
// 加载图像。
using (var stream = new MemoryStream(networkImageDatas))
{
try
{
await SetStreamAsync(stream.AsRandomAccessStream(), requestTime);
}
catch (Exception ex)
{
if (ImageFailed != null)
{
ImageFailed(this, new ImageFailedEventArgs(ex));
}
// 丢弃缓存。
RemoveCachedImage(uri);
IsLoading = false;
return;
}
}
}
else
{
// 本地图像。
if (DesignMode.DesignModeEnabled == false)
{
//.........这里部分代码省略.........
示例8: SaveFile
private async Task SaveFile(StorageFile file)
{
string url = ViewModel.Wallpaper.GetCacheUrl(AppSetting.WallpaperSize);
try
{
using (HttpClient client = new HttpClient())
{
await FileIO.WriteBufferAsync(file, await client.GetBufferAsync(new Uri(url)));
}
await new MessageDialog("保存成功").ShowAsync();
}
catch
{
await new MessageDialog("保存失败").ShowAsync();
}
}