teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/27 14:26

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -1,73 +1,73 @@
1
- FWPでもFPWでもWFPでもなく**WPF**です!(**W**indows **P**resentation **F**oundation)
2
-
3
- `CoreWebView2`ということは`WebView2`ということでしょうかね。
4
-
5
- 普通にurlに直接書いても通りました(`https://user:pass@`)
6
- ちゃんと?やるならこんな感じのようです(コメントを外してください)
7
-
8
- ```xaml
9
- <Window
10
- x:Class="Questions340844.MainWindow"
11
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
12
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
13
- xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
14
- Width="800"
15
- Height="450">
16
- <DockPanel>
17
- <DockPanel DockPanel.Dock="Top">
18
- <Button
19
- Click="ButtonGo_Click"
20
- Content="Go"
21
- DockPanel.Dock="Right" />
22
- <TextBox Name="addressBar" />
23
- </DockPanel>
24
- <wv2:WebView2 Name="webView" Source="https://www.microsoft.com" />
25
- </DockPanel>
26
- </Window>
27
- ```
28
-
29
- ```C#
30
- using Microsoft.Web.WebView2.Core;
31
- using System;
32
- using System.Text;
33
- using System.Windows;
34
-
35
- namespace Questions340844
36
- {
37
- public partial class MainWindow : Window
38
- {
39
- public MainWindow()
40
- {
41
- InitializeComponent();
42
- addressBar.Text = "https://user:pass@httpbin.org/basic-auth/user/pass";
43
-
44
- //InitializeAsync();
45
- }
46
- private async void InitializeAsync()
47
- {
48
- addressBar.Text = "https://httpbin.org/basic-auth/user/pass";
49
-
50
- await webView.EnsureCoreWebView2Async();
51
- webView.CoreWebView2.AddWebResourceRequestedFilter("https://httpbin.org/basic-auth/*", CoreWebView2WebResourceContext.All);
52
- webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
53
- }
54
-
55
- private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
56
- {
57
- var authData = Encoding.UTF8.GetBytes("user:pass");
58
- var authHeader = $"Basic {Convert.ToBase64String(authData)}";
59
- e.Request.Headers.SetHeader("Authorization", authHeader);
60
- }
61
-
62
- private void ButtonGo_Click(object sender, RoutedEventArgs e)
63
- {
64
- if (webView != null && webView.CoreWebView2 != null)
65
- {
66
- webView.CoreWebView2.Navigate(addressBar.Text);
67
- }
68
- }
69
- }
70
- }
71
- ```
72
-
1
+ FWPでもFPWでもWFPでもなく**WPF**です!(**W**indows **P**resentation **F**oundation)
2
+
3
+ `CoreWebView2`ということは`WebView2`ということでしょうかね。
4
+
5
+ 普通にurlに直接書いても通りました(`https://user:pass@`)
6
+ ちゃんと?やるならこんな感じのようです(コメントを外してください)
7
+
8
+ ```xml
9
+ <Window
10
+ x:Class="Questions340844.MainWindow"
11
+ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
12
+ xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
13
+ xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
14
+ Width="800"
15
+ Height="450">
16
+ <DockPanel>
17
+ <DockPanel DockPanel.Dock="Top">
18
+ <Button
19
+ Click="ButtonGo_Click"
20
+ Content="Go"
21
+ DockPanel.Dock="Right" />
22
+ <TextBox Name="addressBar" />
23
+ </DockPanel>
24
+ <wv2:WebView2 Name="webView" Source="https://www.microsoft.com" />
25
+ </DockPanel>
26
+ </Window>
27
+ ```
28
+
29
+ ```cs
30
+ using Microsoft.Web.WebView2.Core;
31
+ using System;
32
+ using System.Text;
33
+ using System.Windows;
34
+
35
+ namespace Questions340844
36
+ {
37
+ public partial class MainWindow : Window
38
+ {
39
+ public MainWindow()
40
+ {
41
+ InitializeComponent();
42
+ addressBar.Text = "https://user:pass@httpbin.org/basic-auth/user/pass";
43
+
44
+ //InitializeAsync();
45
+ }
46
+ private async void InitializeAsync()
47
+ {
48
+ addressBar.Text = "https://httpbin.org/basic-auth/user/pass";
49
+
50
+ await webView.EnsureCoreWebView2Async();
51
+ webView.CoreWebView2.AddWebResourceRequestedFilter("https://httpbin.org/basic-auth/*", CoreWebView2WebResourceContext.All);
52
+ webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
53
+ }
54
+
55
+ private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
56
+ {
57
+ var authData = Encoding.UTF8.GetBytes("user:pass");
58
+ var authHeader = $"Basic {Convert.ToBase64String(authData)}";
59
+ e.Request.Headers.SetHeader("Authorization", authHeader);
60
+ }
61
+
62
+ private void ButtonGo_Click(object sender, RoutedEventArgs e)
63
+ {
64
+ if (webView != null && webView.CoreWebView2 != null)
65
+ {
66
+ webView.CoreWebView2.Navigate(addressBar.Text);
67
+ }
68
+ }
69
+ }
70
+ }
71
+ ```
72
+
73
73
  [c# - Edit HTTP Request header with WebView2 - Stack Overflow](https://stackoverflow.com/questions/62094181/edit-http-request-header-with-webview2)