Web浏览器没有cookie [英] WebBrowser w/o cookies

查看:216
本文介绍了Web浏览器没有cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调用Navigate时,是否可以让Winodws.Form.WebBrowser(VS 8 Beta2 C#)不将站点的Cookie传递给主机?

Cookie可以更改生成的页面和我想获得一个不受cookie影响的页面。

通常使用此控件,Document可用于获取cookie,但这些是响应cookie。 OnNavigating似乎是这样做的地方,但我看不出怎么样?

Is it possible to get the Winodws.Form.WebBrowser (VS 8 Beta2 C#) to not pass the Cookies for a site to the host when calling Navigate?

Cookies can change the resulting page and I would like to get a page that is not influenced by the cookies.

Usually with this control the Document can be used to get the cookies, but these are the response cookies. The OnNavigating seem like the place to do this but I can't see how?

推荐答案

WebBrowser控件没有公开有关cookie或内部状态的任何信息。据我所知,这些内部状态主要由一些可通过COM +访问的外部组件管理。

The WebBrowser control does not expose any information about cookies or internal state. As far as I can tell, these internal states are largely managed by some external component which is accessed through COM+.

但是,检索任何没有cookie的单个网络资源都相当容易。只需使用WebClient或任何其他类似的类下载具有当前URI的资源。

However, retrieving any single web resource without a cookie is rather easy. Simply download the resource with the current URI with a WebClient or any other comparable class.

为方便起见,我已经实现了一个示例:

I've implemented an example for your convenience:


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace WindowsFormsApplication3
{
    public partial class Form1 : Form
    {
        private System.Windows.Forms.WebBrowser webBrowser1;
        private System.Windows.Forms.Button button1;

        public Form1()
        {
            this.webBrowser1 = new System.Windows.Forms.WebBrowser();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // webBrowser1
            // 
            this.webBrowser1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
            | System.Windows.Forms.AnchorStyles.Right)));
            this.webBrowser1.Location = new System.Drawing.Point(0, 0);
            this.webBrowser1.MinimumSize = new System.Drawing.Size(20, 20);
            this.webBrowser1.Name = "webBrowser1";
            this.webBrowser1.Size = new System.Drawing.Size(645, 381);
            this.webBrowser1.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(258, 399);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(136, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "Retrieve without cookie";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(657, 447);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.webBrowser1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            webBrowser1.DocumentText =
            "<html><body>" +            
            "<a href='http://www.live.com'>continue</a>" +
            "</body></html>";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var x = new WebClient();
            x.DownloadDataCompleted += (sender1, args) =>
            {
                webBrowser1.DocumentStream = new MemoryStream(args.Result);
            };
            webBrowser1.DocumentText = "One moment please…";
            x.DownloadDataAsync(webBrowser1.Url);            
        }
    }
}


这篇关于Web浏览器没有cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆