使用 WebBrowser cookie 的 c# WebRequest

发布于 2024-07-15 07:39:45 字数 162 浏览 9 评论 0原文

我正在使用 WebBrowser 登录网站,然后我想使用正则表达式来获取一些数据,但是 webRequest 没有使用 web Browse cookie,

我的 webBrowser 是公开的, 有什么方法可以在 webRequest 中使用 WebBrowser cookie 吗?

I am logging into a site using a WebBrowser, then i want use regex to get some data , but webRequest didnt use web Browse cookie ,

my webBrowser is in public ,
is there any way to using WebBrowser cookie in webRequest ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二智少女 2024-07-22 07:39:45
    public CookieContainer GetCookieContainer()
    {
        CookieContainer container = new CookieContainer();

        foreach (string cookie in webBrowser1.Document.Cookie.Split(';'))
        {
            string name = cookie.Split('=')[0];
            string value = cookie.Substring(name.Length + 1);
            string path = "/";
            string domain = ".google.com"; //change to your domain name
            container.Add(new Cookie(name.Trim(), value.Trim(), path, domain));
        }

        return container;
    }

这适用于大多数网站,但使用子域的网站可能会出现问题。

    public CookieContainer GetCookieContainer()
    {
        CookieContainer container = new CookieContainer();

        foreach (string cookie in webBrowser1.Document.Cookie.Split(';'))
        {
            string name = cookie.Split('=')[0];
            string value = cookie.Substring(name.Length + 1);
            string path = "/";
            string domain = ".google.com"; //change to your domain name
            container.Add(new Cookie(name.Trim(), value.Trim(), path, domain));
        }

        return container;
    }

This will work on most sites, however sites that use subdomains might be a problem.

情独悲 2024-07-22 07:39:45

您可以将 CookieContainer 用于 Web 请求。

 web_cookies = new CookieContainer();
 // Create a 'WebRequest' object with the specified url.                 
 HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url);

 myWebRequest.CookieContainer = web_cookies;

希望这可以帮助。

好的,您想要登录。那是不同的故事。 您可以使用 NetworkCredential 来实现这一点。

public string get_secure_webpage(string url, string username, string password)
    {
        WebRequest myWebRequest = WebRequest.Create(url);
        NetworkCredential networkCredential = new NetworkCredential(username, password);
        myWebRequest.Credentials = networkCredential;

...

You can use a CookieContainer for a Webrequest.

 web_cookies = new CookieContainer();
 // Create a 'WebRequest' object with the specified url.                 
 HttpWebRequest myWebRequest = (HttpWebRequest)WebRequest.Create(url);

 myWebRequest.CookieContainer = web_cookies;

Hope this helps.

Ok, you want to do a log in. Thats is different story. You can use NetworkCredential for that.

public string get_secure_webpage(string url, string username, string password)
    {
        WebRequest myWebRequest = WebRequest.Create(url);
        NetworkCredential networkCredential = new NetworkCredential(username, password);
        myWebRequest.Credentials = networkCredential;

...

好菇凉咱不稀罕他 2024-07-22 07:39:45

这是银光吗?
如果是这样,从 silverlight 3 开始,如果您使用浏览器网络堆栈,那么您应该免费获得 cookie。 默认情况下,当您使用 WebRequest.Create() 方法创建 HttpWebrequest 时,您会获得浏览器堆栈。 请注意,如果您使用 CreateHTTP 方法,您将获得一个客户端堆栈,默认情况下该堆栈不包含浏览器 cookie(您必须采取欺骗手段才能获取它们,如前所述)

请参阅 http://msdn.microsoft.com/en-us/library/dd920295(VS.95).aspx关于 silverlight 自版本 3 以来的网络堆栈

is this silverlight?
if so, since silverlight 3 if you use the browser network stack than you should get cookies for free. By default you get the browser stack when you create n HttpWebrequest using the WebRequest.Create() method. note if you use CreateHTTP method, you get a client stack, which does not include browser cookies by default (you have to do trickery to get them, as described previously)

see http://msdn.microsoft.com/en-us/library/dd920295(VS.95).aspx about the network stacks in silverlight since version 3

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文