Создал такую тему
https://stackoverflow.com/questions/79706333/how-to-specify-login-for-proxy-requiring-authorization-in-webview2
Судя по ответу надо написать:
private async void Form1_Load(object sender, EventArgs e)
{
var options = new CoreWebView2EnvironmentOptions
{
AdditionalBrowserArguments = "--proxy-server=https://address:port"
};
var env = await CoreWebView2Environment.CreateAsync(null, null, options);
await webView21.EnsureCoreWebView2Async(env);
webView21.CoreWebView2.WebResourceRequested += (sender, args) =>
{
string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("username:password"));
args.Request.Headers.SetHeader("Proxy-Authorization", $"Basic {credentials}");
};
webView21.Source = new Uri("https://x.com/elonmusk");
}
Но авторизация прокси не срабатывает.
Я неправильно написал или отвечающий ошибся?
Запустил curl -v -U user:pass -x proxy:port --url
Получил
* Connected to proxy (proxy) port ....
* using HTTP/1.x
* Proxy auth using Basic with user 'user657768'
> GET http://www.google.com/ HTTP/1.1
> Host: www.google.com
> Proxy-Authorization: Basic dFXlcjK3OTI5NTpzbjiqbzQ=
> User-Agent: curl/8.13.0
> Accept: */*
> Proxy-Connection: Keep-Alive
>
* Request completely sent off
< HTTP/1.1 200 OK
сравнил Proxy-Authorization: Basic со строкой string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("user:pass")); Строки совпали.
Т.е. curl успешно проходит авторизацию proxy, которая является Basic, и подсоединяется к сайту. А WebView2 непонятно почему не работает.
Нашел еще
https://stackoverflow.com/questions/62094181/edit-http-request-header-with-webview2/65864195#65864195
Там в конце страницы рекомендуют использовать:
private void Navigate(string url, string uriAdditionalHeader)
{
var httpMethodName = "GET"; // or "POST". As of now only GET and POST is supported as part of NavigateWithWebResourceRequest.
var resourceRequest = this.coreWebView2.Environment.CreateWebResourceRequest(url, "GET", Stream.Null, **uriAdditionalHeader**);
this.coreWebView2.NavigateWithWebResourceRequest(resourceRequest);
}
Но указывал вместо uriAdditionalHeader Proxy-Authorization:строка из string credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("user:pass"));
Нет ответа.
И пытался заменить Proxy-Authorization на Authorization, но все то же.