回答編集履歴

1

見直しキャンペーン中

2023/07/27 14:26

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,145 +1,73 @@
1
1
  FWPでもFPWでもWFPでもなく**WPF**です!(**W**indows **P**resentation **F**oundation)
2
-
3
-
4
2
 
5
3
  `CoreWebView2`ということは`WebView2`ということでしょうかね。
6
4
 
7
-
8
-
9
5
  普通にurlに直接書いても通りました(`https://user:pass@`)
10
-
11
6
  ちゃんと?やるならこんな感じのようです(コメントを外してください)
12
7
 
13
-
14
-
15
- ```xaml
8
+ ```xml
16
-
17
9
  <Window
18
-
19
10
  x:Class="Questions340844.MainWindow"
20
-
21
11
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
22
-
23
12
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
24
-
25
13
  xmlns:wv2="clr-namespace:Microsoft.Web.WebView2.Wpf;assembly=Microsoft.Web.WebView2.Wpf"
26
-
27
14
  Width="800"
28
-
29
15
  Height="450">
30
-
31
16
  <DockPanel>
32
-
33
17
  <DockPanel DockPanel.Dock="Top">
34
-
35
18
  <Button
36
-
37
19
  Click="ButtonGo_Click"
38
-
39
20
  Content="Go"
40
-
41
21
  DockPanel.Dock="Right" />
42
-
43
22
  <TextBox Name="addressBar" />
44
-
45
23
  </DockPanel>
46
-
47
24
  <wv2:WebView2 Name="webView" Source="https://www.microsoft.com" />
48
-
49
25
  </DockPanel>
50
-
51
26
  </Window>
52
-
53
27
  ```
54
28
 
55
-
56
-
57
- ```C#
29
+ ```cs
58
-
59
30
  using Microsoft.Web.WebView2.Core;
60
-
61
31
  using System;
62
-
63
32
  using System.Text;
64
-
65
33
  using System.Windows;
66
34
 
67
-
68
-
69
35
  namespace Questions340844
70
-
71
36
  {
72
-
73
37
  public partial class MainWindow : Window
74
-
75
38
  {
76
-
77
39
  public MainWindow()
78
-
79
40
  {
80
-
81
41
  InitializeComponent();
82
-
83
42
  addressBar.Text = "https://user:pass@httpbin.org/basic-auth/user/pass";
84
43
 
44
+ //InitializeAsync();
45
+ }
46
+ private async void InitializeAsync()
47
+ {
48
+ addressBar.Text = "https://httpbin.org/basic-auth/user/pass";
85
49
 
86
-
87
- //InitializeAsync();
50
+ await webView.EnsureCoreWebView2Async();
88
-
51
+ webView.CoreWebView2.AddWebResourceRequestedFilter("https://httpbin.org/basic-auth/*", CoreWebView2WebResourceContext.All);
52
+ webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
89
53
  }
90
54
 
91
- private async void InitializeAsync()
55
+ private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
92
-
93
56
  {
94
-
57
+ var authData = Encoding.UTF8.GetBytes("user:pass");
95
- addressBar.Text = "https://httpbin.org/basic-auth/user/pass";
58
+ var authHeader = $"Basic {Convert.ToBase64String(authData)}";
96
-
97
-
98
-
99
- await webView.EnsureCoreWebView2Async();
100
-
101
- webView.CoreWebView2.AddWebResourceRequestedFilter("https://httpbin.org/basic-auth/*", CoreWebView2WebResourceContext.All);
102
-
103
- webView.CoreWebView2.WebResourceRequested += CoreWebView2_WebResourceRequested;
59
+ e.Request.Headers.SetHeader("Authorization", authHeader);
104
-
105
60
  }
106
61
 
107
-
108
-
109
- private void CoreWebView2_WebResourceRequested(object sender, CoreWebView2WebResourceRequestedEventArgs e)
62
+ private void ButtonGo_Click(object sender, RoutedEventArgs e)
110
-
111
63
  {
112
-
113
- var authData = Encoding.UTF8.GetBytes("user:pass");
64
+ if (webView != null && webView.CoreWebView2 != null)
114
-
115
- var authHeader = $"Basic {Convert.ToBase64String(authData)}";
65
+ {
116
-
117
- e.Request.Headers.SetHeader("Authorization", authHeader);
66
+ webView.CoreWebView2.Navigate(addressBar.Text);
118
-
67
+ }
119
68
  }
120
-
121
-
122
-
123
- private void ButtonGo_Click(object sender, RoutedEventArgs e)
124
-
125
- {
126
-
127
- if (webView != null && webView.CoreWebView2 != null)
128
-
129
- {
130
-
131
- webView.CoreWebView2.Navigate(addressBar.Text);
132
-
133
- }
134
-
135
- }
136
-
137
69
  }
138
-
139
70
  }
140
-
141
71
  ```
142
72
 
143
-
144
-
145
73
  [c# - Edit HTTP Request header with WebView2 - Stack Overflow](https://stackoverflow.com/questions/62094181/edit-http-request-header-with-webview2)