Web浏览器中的401未经授权的访问

时间:2019-01-16 14:30:12

标签: c# winforms webbrowser-control

我有一个程序可以使用winform和webbrowser创建网站的屏幕截图。 我在下面的字符串中收到错误401未经授权的访问: WebResponse myWebResponse = myWebRequest.GetResponse();

如何通过成功的授权启动URL?

namespace WebBrowserScreenshotSample
{
   class Program
   {
        [STAThread]
        static void Main()
        {
           int width = 800;
           int height = 1200;

            NetworkCredential myCredentials = new NetworkCredential("login@domain", "password");

            WebRequest myWebRequest = WebRequest.Create("URL");
            myCredentials.Domain = "domain";
            myWebRequest.Credentials = myCredentials;

            Console.WriteLine("\n\nCredentials Domain : {0}, UserName : {1}, Password : {2}", myCredentials.Domain, myCredentials.UserName, myCredentials.Password);
            Console.WriteLine("\n\nRequest to Url is sent.Waiting for response...");

            WebResponse myWebResponse = myWebRequest.GetResponse();
            Console.WriteLine("\nResponse received successfully.");

            using (WebBrowser browser = new WebBrowser())
            {
                browser.Width = width;
                browser.Height = height;
                browser.ScrollBarsEnabled = true;
                browser.DocumentCompleted += Program.OnDocumentCompleted;
                browser.Navigate("URL");
                Application.Run();
            }
        }
    }
}

0 个答案:

没有答案