Is it possible to transfer authentication from Webbrowser to WebRequest

后端 未结 9 2096
无人共我
无人共我 2020-11-27 14:10

I\'m using webbrowser control to login any site. And then i want to download some sub page html using WebRequest (or WebClient). This links must requires authentication.

相关标签:
9条回答
  • 2020-11-27 14:40

    If the question is only "How to transfer Webbrowser authentication information to Webrequest or Webclient?" this code is enough:

    You can call the GetUriCookieContainer method that returns you a CookieContainer that can be used for subsequent call with WebRequest object.

      [DllImport("wininet.dll", SetLastError = true)]
        public static extern bool InternetGetCookieEx(
            string url, 
            string cookieName, 
            StringBuilder cookieData, 
            ref int size,
            Int32  dwFlags,
            IntPtr  lpReserved);
    
        private const Int32 InternetCookieHttponly = 0x2000;
    
    /// <summary>
    /// Gets the URI cookie container.
    /// </summary>
    /// <param name="uri">The URI.</param>
    /// <returns></returns>
    public static CookieContainer GetUriCookieContainer(Uri uri)
    {
        CookieContainer cookies = null;
        // Determine the size of the cookie
        int datasize = 8192 * 16;
        StringBuilder cookieData = new StringBuilder(datasize);
        if (!InternetGetCookieEx(uri.ToString(), null, cookieData, ref datasize, InternetCookieHttponly, IntPtr.Zero))
        {
            if (datasize < 0)
                return null;
            // Allocate stringbuilder large enough to hold the cookie
            cookieData = new StringBuilder(datasize);
            if (!InternetGetCookieEx(
                uri.ToString(),
                null, cookieData, 
                ref datasize, 
                InternetCookieHttponly, 
                IntPtr.Zero))
                return null;
        }
        if (cookieData.Length > 0)
        {
            cookies = new CookieContainer();
            cookies.SetCookies(uri, cookieData.ToString().Replace(';', ','));
        }
        return cookies;
    }
    
    0 讨论(0)
  • 2020-11-27 14:41

    You should be able to access the cookies of the WebBrowser control with .Document.Cookie then in your HTTPWebRequest you can add that cookie to its cookie container.
    Here is an example (VB.NET because I'm most familiar there):

    Dim browser As New WebBrowser()
    /*Do stuff here to auth with your webbrowser and get a cookie.*/
    
    Dim myURL As String = "http://theUrlIWant.com/"
    Dim request As New HTTPWebRequest(myURL)
    request.CookieContainer = New CookieContainer()
    request.CookieContainer.SetCookies(myURL, browser.Document.Cookie)
    

    And that should transfer the cookie from your WebBrowser control over to your HTTPWebRequest class.

    0 讨论(0)
  • 2020-11-27 14:41

    A late answer for future references. WebBrowser uses UrlMon library which manages the session per-process, so UrlMon APIs like URLOpenStream or URLDownloadToFile can be used to download any resource on the same session (the APIs can be called from C# via P/invoke). A similar question answered here.

    0 讨论(0)
提交回复
热议问题