-1

I'm trying to download a PDF using WebView2. I can login on the browser but then i would like to download a PDF using WebClient. But this returns me to the login screen. Both WebView browsers use the same UserDataFolder. I would like to download the PDF using the cookie settings used in the WebView2 browser. Below is a sample that i'm using, but i don't know how to use it. I believe i'm on the right track...

Private Sub WV_CoreWebView2Ready1(sender As Object, e As EventArgs) Handles WebView_Browser1.CoreWebView2InitializationCompleted

WebView_Browser1.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All)
AddHandler WebView_Browser1.CoreWebView2.WebResourceRequested, AddressOf CoreWebView2_WebResourceRequested

End Sub

Private Sub CoreWebView2_WebResourceRequested(ByVal sender As Object, ByVal e As CoreWebView2WebResourceRequestedEventArgs)

e.Request.Headers.SetHeader("Cookie", XXXXX) <<< i don't know how to use this
Dim addedDate = e.Request.Headers.GetHeader("Cookie")

End Sub

Private Async Sub WebView_Browser2_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles WebView_Browser2.NavigationCompleted

Dim url As String = pathtourl
Dim fileName As String = "pdf.pdf"
Dim webClient As WebClient = New WebClient()
webClient.Headers.Add(HttpRequestHeader.Cookie, WebView_Browser2.Cookie) <<< use cookie here to login?
webClient.DownloadFileAsync(New Uri(url, UriKind.Absolute), fileName)

End Sub

It would be great i somebody can help me with this! Thanks!

Update

Dim LoginCookie As String

Private Sub WV_CoreWebView2Ready1(sender As Object, e As EventArgs) Handles WebView_Browser1.CoreWebView2InitializationCompleted

WebView_Browser1.CoreWebView2.AddWebResourceRequestedFilter("*", CoreWebView2WebResourceContext.All)
AddHandler WebView_Browser1.CoreWebView2.WebResourceRequested, AddressOf CoreWebView2_WebResourceRequested
AddHandler WebView_Browser1.CoreWebView2.WebResourceResponseReceived, AddressOf CoreWebView2_WebResourceResponseReceived

End Sub

Private Sub CoreWebView2_WebResourceResponseReceived(ByVal sender As Object, ByVal e As CoreWebView2WebResourceResponseReceivedEventArgs)

LoginCookie = e.Request.Headers.GetHeader("Login")

End Sub

Private Async Sub WebView_Browser2_NavigationCompleted(sender As Object, e As CoreWebView2NavigationCompletedEventArgs) Handles WebView_Browser2.NavigationCompleted

Dim url As String = pathtourl
Dim fileName As String = "pdf.pdf"
Dim webClient As WebClient = New WebClient()
webClient.Headers.Add(HttpRequestHeader.Cookie, LoginCookie)
webClient.DownloadFileAsync(New Uri(url, UriKind.Absolute), fileName)

End Sub

The Webclient accepts the cookie to be webClient.Headers.Add(HttpRequestHeader.Cookie, "CITY=NY") like this. But the e.Request.Headers.GetHeader("Login") doesn't get me the cookie.

Again thank you for looking!

Desz
  • 31
  • 7
  • I'm not sure I totally understand your scenario. You use WebView2 to have the end user login, then you want to use WebClient to download a PDF and you need to get the cookies set on WebView2 and apply them to the WebClient? You can use CoreWebView2.CookieManager to get and set cookies on the WebView2. Which cookies you need probably depends on the login page. https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2.cookiemanager?view=webview2-dotnet-1.0.961.33 – David Risney Oct 04 '21 at 20:17
  • Yes, that's my scenario. I would like to download the PDF. Do you maybe have an example for me how to use the CoreWebView2.CookieManager in my code? Thank you! – Desz Oct 04 '21 at 20:39
  • Maybe in your CoreWebView2.NavigationCompleted handler for the navigation corresponding to the page that comes after the end user logs in, you can do CoreWebView2.CookieManager.GetCookies("https://.../") to get the cookies for that page. You can either pick out the auth related ones or perhaps copy them all over to the WebClient? I'm not a WebClient expert. CoreWebView2Cookie does have a ToSystemNetCookie method if that's helpful. https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2cookie.tosystemnetcookie?view=webview2-dotnet-1.0.961.33 – David Risney Oct 04 '21 at 21:46
  • @DavidRisney I've updated the question, maybe you can see what i'm doing wrong here. I've also put in a url of the page after login as url after the cookiemanager but it still doesn't get cookies. Thanks! – Desz Oct 05 '21 at 17:59
  • The WebView2 part looks OK. If you debug are you getting the cookies you expect? The call to webClient.HEaders.Add(..., cookies) I wouldn't expect to work as is. First, your cookies variable is scoped to just that first sub isn't it? Second, the cookies variable is a list of CoreWebView2Cookie, which is not what Headers.Add takes. You'll need to turn those cookies into something that Headers.Add accepts. I'm not a WebClient expert though. – David Risney Oct 05 '21 at 18:24
  • @DavidRisney I've updated the question yet again with my current situation. To answer your questions, I get the cookies when i debug, the cookie variable is scoped to the first sub but when i put it in a string i can pass it on. It would be great if you can help me! Thank you! – Desz Oct 08 '21 at 19:11

1 Answers1

0

You can use this to get the cookies in C# similar pattern for VB.NET

List<CoreWebView2Cookie> cookieList = await WebBrowser.CoreWebView2.CookieManager.GetCookiesAsync(CurrentAddress);

Then loop through to get the string values you are looking for

for (int i = 0; i < cookieList.Count; ++i)
{
    CoreWebView2Cookie cookie = TopicWebBrowser.CoreWebView2.CookieManager.CreateCookieWithSystemNetCookie(cookieList[i].ToSystemNetCookie());

    cookieResult.Append($"\n{cookie.Name} {cookie.Value} {(cookie.IsSession ? "[session cookie]" : cookie.Expires.ToString("G"))}");
}
cigien
  • 57,834
  • 11
  • 73
  • 112
bluejay
  • 95
  • 1
  • 6