public async Task <StorageFile> Load() { try { Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient(); var buffer = await httpClient.GetBufferAsync(new Uri("http://www.neu.edu.cn/indexsource/neusong.mp3")); if (buffer != null && buffer.Length > 0u) { var file2 = await KnownFolders.MusicLibrary.CreateFileAsync("neusong.mp3", CreationCollisionOption.ReplaceExisting); using (var stream = await file2.OpenAsync(FileAccessMode.ReadWrite)) { await stream.WriteAsync(buffer); await stream.FlushAsync(); } mediaPlayer.Source = MediaSource.CreateFromStorageFile(file2); return(file2); } } catch { } return(null); }
/// <summary> /// 获取Bing图片。 /// </summary> /// <returns>返回BitmapImage图片</returns> public async Task <BitmapImage> GetBitmapImageAsync() { GenerateBgService bgService = new GenerateBgService(); BingImageData bingImageData = Task.Run(bgService.getBingImageDataAsync).Result; Image item = bingImageData.images[0]; Windows.Web.Http.HttpClient http = new Windows.Web.Http.HttpClient(); string uri = "https://www.bing.com" + item.url; IBuffer buffer = await http.GetBufferAsync(new Uri(uri)); BitmapImage img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(buffer); stream.Seek(0); await img.SetSourceAsync(stream); } // Save the bing image to local folder SaveImageToLocalFolder(uri); return(img); }
public static async Task <BitmapImage> DownloadFile(Uri uri) { Windows.Web.Http.HttpClient http = new Windows.Web.Http.HttpClient(); try { IBuffer buffer = await http.GetBufferAsync(uri); BitmapImage img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(buffer); stream.Seek(0); await img.SetSourceAsync(stream); await StorageImageFolder(stream, uri); return(img); } } catch (Exception) { return(null); } finally { http.Dispose(); } }
public static async Task <StorageFile> DownloadImageFromUri(Uri uri, BackgroundTaskType type) { // Save the image to storage Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(); var referer = uri.ToString(); client.DefaultRequestHeaders.Add("Referer", referer); client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (IE 11.0; Windows NT 6.3; Trident/7.0; .NET4.0E; .NET4.0C; rv:11.0) like Gecko"); var downloadTask = client.GetBufferAsync(uri); var resultBuffer = (await downloadTask); StorageFolder localFolder = ApplicationData.Current.LocalFolder; var imageFolder = await localFolder.CreateFolderAsync(type == BackgroundTaskType.LockScreen? "Lockscreens" : "Wallpapers", CreationCollisionOption.OpenIfExists); var imageFiles = await imageFolder.GetFilesAsync(); foreach (var oldImageFile in imageFiles) { try { await oldImageFile.DeleteAsync(StorageDeleteOption.PermanentDelete); } catch (Exception ex) { } } var imageFile = await imageFolder.CreateFileAsync(DateTime.UtcNow.Ticks.ToString() + ".jpg", CreationCollisionOption.ReplaceExisting); await FileIO.WriteBufferAsync(imageFile, resultBuffer); return(imageFile); }
private async void DownloadMedia_ClickAsync(object sender, RoutedEventArgs e) { Uri uri = new Uri(Uri.Text); using (var httpClient = new Windows.Web.Http.HttpClient()) { // Always catch network exceptions for async methods try { var downloadMusic = await httpClient.GetBufferAsync(uri); StorageFile destinationFile = await KnownFolders.MusicLibrary.CreateFileAsync("123.mp3", CreationCollisionOption.GenerateUniqueName); using (var stream = await destinationFile.OpenAsync(FileAccessMode.ReadWrite)) { await stream.WriteAsync(downloadMusic); await stream.FlushAsync(); } } catch (Exception) { } } }
private async void setData() { link.NavigateUri = new System.Uri(jo["data"][index]["url"].ToString()); during.Text = jo["data"][index]["durationMin"].ToString() + "分钟"; detail.Text = jo["data"][index]["description"].ToString(); ltitle.Text = jo["data"][index]["titleAliases"][0].ToString(); screen.Text = jo["data"][index]["screenTypes"][0].ToString(); var urip = new System.Uri(jo["data"][index]["coverUrl"].ToString()); Windows.Web.Http.HttpClient http = new Windows.Web.Http.HttpClient(); IBuffer buffer = await http.GetBufferAsync(urip); BitmapImage img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(buffer); stream.Seek(0); await img.SetSourceAsync(stream); rounding.Visibility = Windows.UI.Xaml.Visibility.Collapsed; cover.Source = img; } movie.Visibility = Visibility.Visible; title.Text = jo["data"][index]["title"].ToString(); style.Text = string.Join("/", jo["data"][index]["genres"]); rating.Value = (int)double.Parse(jo["data"][index]["rating"].ToString()) / 2; }
/** * 从服务器请求个人信息 * 显示个人信息页面信息 */ private async void GetInfo() { string jsonString = "{ \"user\" : {\"username\":\"" + viewModel.CurrentUser.username + "\"} }"; string result = await Models.Post.PostHttp("/user_get", jsonString); JObject data = JObject.Parse(result); viewModel.CurrentUser.username = data["user"]["username"].ToString(); viewModel.CurrentUser.Password = Post.DecodePsd(data["user"]["password"].ToString()); viewModel.CurrentUser.Mail = data["user"]["email"].ToString(); viewModel.CurrentUser.Phone = data["user"]["tel"].ToString(); viewModel.CurrentUser.QQ = data["user"]["qq"].ToString(); viewModel.CurrentUser.Wechat = data["user"]["wechat"].ToString(); viewModel.CurrentUser.PhotoUrl = data["user"]["icon"].ToString(); if (viewModel.CurrentUser.PhotoUrl != "") { Windows.Web.Http.HttpClient http = new Windows.Web.Http.HttpClient(); IBuffer buffer = await http.GetBufferAsync(new Uri("http://" + viewModel.CurrentUser.PhotoUrl)); BitmapImage img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(buffer); stream.Seek(0); await img.SetSourceAsync(stream); photo.ImageSource = img; } } }
public async Task <IBuffer> getBufferHttp(string url) { Windows.Web.Http.HttpClient http = new Windows.Web.Http.HttpClient(); string content = ""; var response = await http.GetBufferAsync(new Uri(url)); return(response); }
public static async void SaveHttpImage(string folder, string fileName, string url, Action action = null) { try { Windows.Web.Http.HttpClient http = new Windows.Web.Http.HttpClient(); IBuffer buffer = await http.GetBufferAsync(new Uri(url)); BitmapImage img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(buffer); stream.Seek(0); await img.SetSourceAsync(stream); var storageFolder = await StorageFolder.GetFolderFromPathAsync(folder); StorageFile file = await storageFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); DataReader read = new DataReader(stream.GetInputStreamAt(0)); await read.LoadAsync((uint)stream.Size); byte[] temp = new byte[stream.Size]; read.ReadBytes(temp); await FileIO.WriteBytesAsync(file, temp); action?.Invoke(); } } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.Message + "\n" + ex.StackTrace); Debug.WriteLine(url); if (File.Exists(Path.Combine(folder, fileName))) { File.Delete(Path.Combine(folder, fileName)); } } }
private async void selectfilebutton_ClickAsync(object sender, RoutedEventArgs e) { Uri uri = new Uri(selectfiletext.Text); if (((Button)sender).Name == "selectfileplay") { mainplayer.Source = uri; musicpic.Opacity = 100; selectfileinfo.Text = "正在播放"; } else//下载文件 { var myMusics = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Music); var myMusic = myMusics.SaveFolder; //var myMusic = KnownFolders.MusicLibrary; var fileName = Path.GetFileName(uri.LocalPath); try { StorageFile musicFile = await myMusic.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.FailIfExists); if (musicFile != null) { Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient(); IBuffer buffer; try { buffer = await httpClient.GetBufferAsync(uri); } catch (Exception ex) { selectfileinfo.Text = "下载失败"; return; } await FileIO.WriteBufferAsync(musicFile, buffer); selectfileinfo.Text = "下载完成"; } } catch (Exception ex) { selectfileinfo.Text = "文件已存在"; } } }
private async void downLoad_Click(object sender, RoutedEventArgs e) { if (Uri.IsWellFormedUriString(source.Text, UriKind.Absolute)) { Uri uri = new Uri(source.Text); var myMusics = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Music); var myMusic = myMusics.SaveFolder; var fileName = Path.GetFileName(uri.LocalPath); try { StorageFile musicFile = await myMusic.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.FailIfExists); if (musicFile != null) { Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient(); IBuffer buffer; try { buffer = await httpClient.GetBufferAsync(uri); } catch (Exception ex) { Tips.Text = "download fail!"; return; } await FileIO.WriteBufferAsync(musicFile, buffer); Tips.Text = "download complete!"; mediaPlayer.Source = MediaSource.CreateFromUri(new Uri(source.Text)); } } catch (Exception ex) { Tips.Text = "file exist"; mediaPlayer.Source = MediaSource.CreateFromUri(new Uri(source.Text)); } } else { Tips.Text = "Invalid url!"; } }
// 从网络获取图片 private static async Task <BitmapImage> GetHttpImage(Uri uri) { try { var http = new Windows.Web.Http.HttpClient(); var buffer = await http.GetBufferAsync(uri); var img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(buffer); stream.Seek(0); await img.SetSourceAsync(stream); await StorageImageFolder(stream, uri); return(img); } } catch (Exception) { return(null); } }
public static async Task <BitmapImage> GetWebImageAsync(string url) { try { Windows.Web.Http.HttpClient http = new Windows.Web.Http.HttpClient(); IBuffer buffer = await http.GetBufferAsync(new Uri(url)); BitmapImage img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(buffer); stream.Seek(0); await img.SetSourceAsync(stream); return(img); } } catch { return(null); } }
private async void button_2_Click(object sender, RoutedEventArgs e) { Uri uri = new Uri("http://www.neu.edu.cn/indexsource/neusong.mp3"); var myMusics = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Music); var myMusic = myMusics.SaveFolder; var fileName = Path.GetFileName(uri.LocalPath); try { StorageFile musicFile = await myMusic.CreateFileAsync(fileName, Windows.Storage.CreationCollisionOption.FailIfExists); if (musicFile != null) { Windows.Web.Http.HttpClient httpClient = new Windows.Web.Http.HttpClient(); IBuffer buffer; try { buffer = await httpClient.GetBufferAsync(uri); } catch (Exception ex) { text.Text = "download fail"; return; } await FileIO.WriteBufferAsync(musicFile, buffer); text.Text = "download complete"; } } catch (Exception ex) { text.Text = "file exist"; } }
private async void WebMovieSearchBtn_Click(object sender, RoutedEventArgs e) { WebMovieTitle.Text = WebMovieSearchBar.Text; SideTitle.Text = WebMovieSearchBar.Text; Uri uri = new Uri("http://v.juhe.cn/movie/index?key=2c9349b8af4d9d07c10a371e7a0cf0b0&title=" + WebMovieSearchBar.Text); HttpClient client = new HttpClient(); string result = await client.GetStringAsync(uri); // the following strings can be used as json text in order to save some api call //string result = "{\"resultcode\":\"200\",\"reason\":\"成功的返回\",\"result\":[{\"movieid\":\"232603\",\"rating\":\" - 1\",\"genres\":\"动作\\/ 悬疑\\/ 犯罪\",\"runtime\":null,\"language\":\"汉语普通话\\/ 粤语\",\"title\":\"拆弹专家\",\"poster\":\"http:\\/\\/ img31.mtime.cn\\/ mg\\/ 2016\\/ 06\\/ 06\\/ 113814.34913831_270X405X4.jpg\",\"writers\":\"\",\"film_locations\":\"中国 | 中国香港\",\"directors\":\"邱礼涛\",\"rating_count\":\"6\",\"actors\":\"刘德华 Andy Lau,姜武 Wu Jiang,宋佳 Jia Song\",\"plot_simple\":\"电影讲述了发生在香港红磡隧道里的一次拆弹任务,刘德华扮演一名警队的拆弹专家,要解除姜武饰演的通缉犯制造的炸弹恐怖袭击事件危机,并试图将其抓获。\",\"year\":\"2017\",\"country\":\"中国 | 中国香港\",\"type\":null,\"release_date\":\"20170101\",\"also_known_as\":\"\"}],\"error_code\":0}"; //string result = "{\"resultcode\":\"200\",\"reason\":\"成功的返回\",\"result\":[{\"movieid\":\"147502\",\"rating\":\"8.3\",\"genres\":\"历史\\/剧情\",\"runtime\":null,\"language\":\"英语\",\"title\":\"泰坦尼克号\",\"poster\":\"http:\\/\\/img21.mtime.cn\\/mt\\/2012\\/03\\/27\\/105223.21161030_270X405X4.jpg\",\"writers\":\"朱利安·费罗斯\",\"film_locations\":\"美国|加拿大|匈牙利|英国\",\"directors\":\"\",\"rating_count\":\"413\",\"actors\":\"Ben Bishop,杰拉丁·萨莫维尔 Geraldine Somerville,李·罗斯 Lee Ross,佩尔迪达·维克斯 Perdita Weeks\",\"plot_simple\":\"总剧情 ITV版《泰坦尼克号》聚焦不同的阶层的乘客、船员,通过多视角来展现泰坦尼克号当时的方方面面。观众们将和角色们一起经历泰坦尼克号沉船前的几个小时,经历那从愉悦到悲痛的落差感以及生离死别。主人公们地位的差别,政治的倾向也许直接影响到他们的生死,还有男女之间的一见钟情和夫妻之间的相濡以沫,可以说,泰坦尼克号的上人物,完全可以 展开 ITV版《泰坦尼克号》聚焦不同的阶层的乘客、船员,通过多视角来展现泰坦尼克号当时的方方面面。观众们将和角色们一起经历泰坦尼克号沉船前的几个小时,经历那从愉悦到悲痛的落差感以及生离死别。主人公们地位的差别,政治的倾向也许直接影响到他们的生死,还有男女之间的一见钟情和夫妻之间的相濡以沫,可以说,泰坦尼克号的上人物,完全可以展现当时英国资产阶级的众生相。\",\"year\":\"2012\",\"country\":\"美国|加拿大|匈牙利|英国\",\"type\":null,\"release_date\":\"20120412\",\"also_known_as\":\"\"},{\"movieid\":\"92572\",\"rating\":\"7.8\",\"genres\":\"动作\\/历史\\/剧情\",\"runtime\":\"85 min\",\"language\":\"德语\",\"title\":\"泰坦尼克号\",\"poster\":\"http:\\/\\/img31.mtime.cn\\/mt\\/2014\\/02\\/23\\/064712.94207588_270X405X4.jpg\",\"writers\":\"Harald Bratt,Hansi Köck,...\",\"film_locations\":\"德国\",\"directors\":\"Herbert Selpin,Werner Klingler\",\"rating_count\":\"19\",\"actors\":\"Sybille Schmitz,Hans Nielsen,Kirsten Heiberg,Ernst Fritz Fürbringer\",\"plot_simple\":null,\"year\":\"1943\",\"country\":\"德国\",\"type\":null,\"release_date\":\"19431110\",\"also_known_as\":\"\"},{\"movieid\":\"40591\",\"rating\":null,\"genres\":\"爱情\\/动作\\/剧情\",\"runtime\":\"173 min \\/ Finland:163 min (DVD)\",\"language\":\"英语\",\"title\":\"泰坦尼克号\",\"poster\":\"http:\\/\\/img31.mtime.cn\\/mt\\/2014\\/02\\/23\\/032605.90912094_270X405X4.jpg\",\"writers\":\"Ross LaManna,Joyce Eliason\",\"film_locations\":\"美国|加拿大\",\"directors\":\"罗伯特·里伯曼\",\"rating_count\":null,\"actors\":\"Eric Schneider,Ron Halder,Byron Lucas,Peter Haworth\",\"plot_simple\":null,\"year\":\"1996\",\"country\":\"美国|加拿大\",\"type\":null,\"release_date\":\"19961117\",\"also_known_as\":\"\"},{\"movieid\":\"28986\",\"rating\":\"8.1\",\"genres\":\"\",\"runtime\":null,\"language\":null,\"title\":\"泰坦尼克号\",\"poster\":\"http:\\/\\/img31.mtime.cn\\/mt\\/986\\/28986\\/28986_270X405X4.jpg\",\"writers\":\"\",\"film_locations\":\"意大利\",\"directors\":\"Pier Angelo Mazzolotti\",\"rating_count\":\"33\",\"actors\":\"Luigi Duse,Giovanni Casaleggio,马里奥·伯纳德 Mario Bonnard\",\"plot_simple\":null,\"year\":\"1915\",\"country\":\"意大利\",\"type\":null,\"release_date\":\"0\",\"also_known_as\":\"\"},{\"movieid\":\"11925\",\"rating\":\"8.9\",\"genres\":\"剧情\\/爱情\",\"runtime\":\"194分钟\",\"language\":\"英语\",\"title\":\"泰坦尼克号\",\"poster\":\"http:\\/\\/img21.mtime.cn\\/mt\\/2012\\/04\\/06\\/101417.97070113_270X405X4.jpg\",\"writers\":\"詹姆斯·卡梅隆\",\"film_locations\":\"美国\",\"directors\":\"詹姆斯·卡梅隆\",\"rating_count\":\"61108\",\"actors\":\"莱昂纳多·迪卡普里奥 Leonardo DiCaprio,凯特·温丝莱特 Kate Winslet,比利·赞恩 Billy Zane,格劳瑞亚·斯图尔特 Gloria Stuart\",\"plot_simple\":\"影片以1912年泰坦尼克号邮轮在其处女启航时触礁冰山而沉没的事件为背景,描述了处于不同阶层的两个人——穷画家杰克和贵族女露丝抛弃世俗的偏见坠入爱河,最终杰克把生命的机会让给了露丝的感人故事。\",\"year\":\"1997\",\"country\":\"美国\",\"type\":null,\"release_date\":\"20120410\",\"also_known_as\":\"铁达尼号\"}],\"error_code\":0}"; //string result = "{\"resultcode\":\"200\",\"reason\":\"成功的返回\",\"result\":[],\"error_code\":0}"; JsonTextReader json = new JsonTextReader(new StringReader(result)); json.Read(); json.Read(); json.Read(); json.Read(); json.Read(); json.Read(); json.Read(); json.Read(); json.Read(); // if the "result" in JSON is empty if (json.Value.ToString() == "error_code") { WebMovieInfo.Text = "Unsuccessful search. Please try another name."; return; } // if there are more than one results, show the first one bool isSetInfo = false; bool isSetPoster = false; while (json.Read()) { if (json.Value != null && json.Value.Equals("plot_simple")) { json.Read(); if (json.Value != null && !isSetInfo) { WebMovieInfo.Text = json.Value.ToString(); SideReview.Text = WebMovieInfo.Text; isSetInfo = true; } } if (json.Value != null && json.Value.Equals("poster")) { json.Read(); if (json.Value != null && !isSetPoster) { string rawUri = json.Value.ToString(); rawUri = rawUri.Replace(" ", ""); Uri imageUri = new Uri(rawUri); Windows.Web.Http.HttpClient http = new Windows.Web.Http.HttpClient(); IBuffer buffer = await http.GetBufferAsync(imageUri); BitmapImage img = new BitmapImage(); using (IRandomAccessStream stream = new InMemoryRandomAccessStream()) { await stream.WriteAsync(buffer); stream.Seek(0); await img.SetSourceAsync(stream); } WebMoviePoster.Source = img; isSetPoster = true; } } } }
// </SnippetAMSDownloadRequested> private Task <IBuffer> CreateMyCustomManifest(Uri resourceUri) { httpClient = new Windows.Web.Http.HttpClient(); return(httpClient.GetBufferAsync(resourceUri).AsTask <IBuffer, Windows.Web.Http.HttpProgress>()); }