将密码传递给Gmail起诉C#时出现空例外 [英] Null Exception while passing password to Gmail suing C#

查看:81
本文介绍了将密码传递给Gmail起诉C#时出现空例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试创建一个网页表单,点击按钮将在IE中打开Gmail。



下面是我正在使用的代码,当我运行它并点击打开Gmail按钮时,我在下划线处收到错误。



我试过Passwd和passwd隐藏但是得到相同的错误。



另外,我想知道如何从安全的地方读取密码而不是用代码硬编码。



感谢您提前的帮助。



  private   void  button2_Click( object  sender,EventArgs e)
{


// 第二次尝试
// 打开Gmail
InternetExplorer explorer = new InternetExplorer();

explorer.DocumentComplete + = OnIEDocumentComplete;

// 将documentComplete事件设置为false
documentComplete = new AutoResetEvent( false );

object mVal = System.Reflection.Missing.Value;
explorer.Navigate( http://www.gmail.com ref mVal, ref mVal, ref mVal,< span class =code-keyword> ref mVal);

// 等待文档完全加载
documentComplete .WaitOne();

HTMLDocument doc =(HTMLDocument)explorer.Document;

HTMLInputElement userID =(HTMLInputElement)doc.all.item( Email 0 );
userID。 value = abc @ gmail .COM;
HTMLInputElement pwd =(HTMLInputElement)doc.all.item( Passwd-hidden 0 );
pwd。 value = abcdef;
HTMLInputElement btnsubmit =(HTMLInputElement)doc.all.item( signIn 0 );
btnsubmit.click();
explorer.Visible = true ;
}





错误:



未处理的例外情况在WindowsFormsApplication1.exe中输入'System.NullReferenceException'



附加信息:对象引用未设置为对象的实例。

解决方案

如果你在这一行收到错误:

 pwd。 value  =   abcdef; 

然后 pwd 必须为空。所以看看它的设置位置 - 在这种情况下如上所示:

 HTMLInputElement pwd =(HTMLInputElement)doc.all.item(  Passwd-hidden 0 ); 
pwd。 value = abcdef< /跨度>;

显然 doc.all.item 返回 null ,这几乎是肯定意味着它找不到一个名为Passwd-hidden的匹配项目。



所以检查页面数据,找出它的名字!


Hi,

I am trying to create a web form where clicking on button will open Gmail in IE.

Below is the code I am using, when I run it and click on Open Gmail button I am getting error at underlined line.

I have tried both Passwd and passwd-hidden but get same error.

Also, I want to know how can I read my password from a secure place and not hardcode it in code.

Thanks for your help in advance.

private void button2_Click(object sender, EventArgs e)
        {


            //second try
            //open Gmail
            InternetExplorer explorer = new InternetExplorer();

            explorer.DocumentComplete += OnIEDocumentComplete;

            // Setting the documentComplete Event to false
            documentComplete = new AutoResetEvent(false);

            object mVal = System.Reflection.Missing.Value;
            explorer.Navigate("http://www.gmail.com", ref mVal, ref mVal, ref mVal, ref mVal);

            // Waiting for the document to load completely
            documentComplete.WaitOne();

            HTMLDocument doc = (HTMLDocument)explorer.Document;

            HTMLInputElement userID = (HTMLInputElement)doc.all.item("Email", 0);
            userID.value = "abc@gmail.com";
            HTMLInputElement pwd = (HTMLInputElement)doc.all.item("Passwd-hidden", 0);
            pwd.value = "abcdef";
            HTMLInputElement btnsubmit = (HTMLInputElement)doc.all.item("signIn", 0);
            btnsubmit.click();
            explorer.Visible = true;
        }



Error:

An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe

Additional information: Object reference not set to an instance of an object.

解决方案

If you are getting the error on this line:

pwd.value = "abcdef";

Then pwd has to be null. So look at where it is set - in teh line above in this case:

HTMLInputElement pwd = (HTMLInputElement)doc.all.item("Passwd-hidden", 0);
pwd.value = "abcdef";

So clearly doc.all.item is returning null, which almost certainly means it can't find a matching item called "Passwd-hidden".

So check the page data, and find out what it is called!


这篇关于将密码传递给Gmail起诉C#时出现空例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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