【问题标题】:How to read cookie files in c# thread如何在 C# 线程中读取 cookie 文件
【发布时间】:2013-05-05 07:49:21
【问题描述】:

我有这个在 C# 中读取 cookie 文件的功能:

private void getdocument()
{
 while (true)
 {
 MessageBox.Show(webBrowser1.Document.Cookie);
 }
}

我是这样调用它的:

private void Form1_Load(object sender, EventArgs e)
{
 Thread MyThread = new Thread(new ThreadStart(getdocument));
 MyThread.Start();
}

但我收到一个错误:

System.InvalidCastException 未处理

指定的转换无效。

如何解决这个问题?

【问题讨论】:

  • 这个问题与C编程语言无关。 CC# 完全不同。
  • 这将启动无限数量的消息框。那是你要的吗?您还从与 UI 线程不同的线程访问浏览器控件。
  • 不,我使用 msgboxes 进行测试。我想在创建时读取 cookie 数据。这就是我使用线程的原因。

标签: c# multithreading c#-4.0


【解决方案1】:

正如您在 Object Hierarchy 结构中看到的那样,WebBrowser 继承自 System.Windows.Forms.Control,因此您需要从创建它的线程中获取它。

修复:

private void getdocument()
{
    while (true)
    {

        this.Invoke((MethodInvoker) delegate
            {
                MessageBox.Show(webBrowser1.Document.Cookie);
            });
    }
}

【讨论】:

  • 它会产生以下错误:System.NullReferenceException 未被用户代码处理——对象引用未设置为对象的实例。但它在“while”之后的“if”条件下运行良好。
  • @user2351414 - 空引用是因为您还没有打开任何网站!在尝试获取 cookie 之前,打开一个 url :)
猜你喜欢
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 2019-06-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多