Ejemplo n.º 1
0
        private async void CoreWebView2_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
        {
            // URL の末尾が jpg か png のみ、画像を保存する。
            if (e.Request.Uri.EndsWith("jpg") || e.Request.Uri.EndsWith("png"))
            {
                //Debug.WriteLine($"WebResourceResponseReceived: {e.Request.Uri}");

                // フォルダが無ければ作成する。
                if (!File.Exists("img"))
                {
                    Directory.CreateDirectory("img");
                }

                var uri = new Uri(e.Request.Uri);

                // 非同期でレスポンス画像を取得する。
                using (var stream = await e.Response.GetContentAsync())
                {
                    using (var fileStream = new FileStream($"img/{uri.Segments.Last()}", FileMode.Create, FileAccess.Write))
                    {
                        // ストリームをファイルに保存する。
                        stream.CopyTo(fileStream);
                    };
                };
            }
        }
Ejemplo n.º 2
0
        private async void CoreWebView2_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
        {
            if (e.Response != null)
            {
                int    statusCode = e.Response.StatusCode;
                string uri        = e.Request.Uri;

                // Save app icons
                if (uri.Contains("/app-icons/") && uri.Contains("size=256"))
                {
                    if (uri.IndexOf("?") != -1)
                    {
                        uri = uri.Substring(0, uri.IndexOf("?"));
                    }

                    string filename = uri.Substring(uri.IndexOf("/app-icons/") + 11);
                    filename = filename.Substring(0, filename.IndexOf("/")) + Path.GetExtension(uri);

                    if (statusCode == 200)
                    {
                        Stream content = await e.Response.GetContentAsync();

                        Stream fileStream = File.OpenWrite(Path.Combine(Program.appDataPath, "AppIcons", filename));
                        await content.CopyToAsync(fileStream);

                        content.Dispose();
                        fileStream.Dispose();
                    }

                    web_loadedAppIcons.Add(Path.GetFileNameWithoutExtension(filename));
                }

                // Save asset icons
                else if (uri.Contains("/app-assets/"))
                {
                    // https://cdn.discordapp.com/app-assets/appId/assetIconId.png
                    string filename = uri.Substring(uri.IndexOf("/app-assets/") + 12).Replace('/', '_');

                    if (statusCode == 200)
                    {
                        Stream content = await e.Response.GetContentAsync();

                        Stream fileStream = File.OpenWrite(Path.Combine(Program.appDataPath, "AppIcons", filename));
                        await content.CopyToAsync(fileStream);

                        content.Dispose();
                        fileStream.Dispose();
                    }

                    web_loadedAppIcons.Add(Path.GetFileNameWithoutExtension(filename));
                }
            }
        }
Ejemplo n.º 3
0
 private void _vistaHtml_WebResourceResponseReceived(object sender, CoreWebView2WebResourceResponseReceivedEventArgs e)
 {
     if (this._tiempos != null)
     {
         this._tiempos.Add(new EntryOfTime
         {
             Etiqueta = "RECURSO SOLICITADO OBTENIDO",
             Mensaje  = "Se obtiene el recurso " + e.Request.Uri,
             Tiempo   = DateTime.Now
         });
         this.DibujaDiagrama();
     }
 }