想实现自动登陆校内网,并写日记的功能;
下面的代码无法实现自动登陆
请大家帮忙看下,谢谢
        //根据用户名和密码,登陆校内网
        private void btnBrowser_Click(object sender, EventArgs e)
        {
            //登陆校内网的网址
            txtURL.Text = "http://login.xiaonei.com/Login.do"; 
            string tmpURL = txtURL.Text.Trim();
            webBrowser1.Navigate(tmpURL);
            //下面的代码无法执行
            //未处理的“System.NullReferenceException”类型的异常出现在 webRefresh.exe 中。
            //其他信息: 未将对象引用设置到对象的实例。
            

            HtmlElement btnSubmit = webBrowser1.Document.All["submit"];
            HtmlElement tbUserid = webBrowser1.Document.All["email"];
            HtmlElement tbPasswd = webBrowser1.Document.All["password"];            tbUserid.SetAttribute("value", "[email protected]"); //用户名
            tbPasswd.SetAttribute("value", "rfv123");               //密码
            btnSubmit.InvokeMember("click"); 
        }
校内网登陆页面部分Html代码
//////////////////////////////////////////////////////////////////////////////////////<label for="email">帐号:</label>
<input type="text" name="email" tabindex="1" value="" id="email" class="input-text"></p>
<p class="clearfix"><label for="password">密码:</label>
<input type="password" id="password" name="password" 
class="input-text" tabindex="2" />
</p><p class="right"><p class="right"><input type="hidden" name="origURL" value="http://www.renren.com/Home.do" />
<input type="hidden" name="domain" value="renren.com" /><input type="hidden" name="formName" value="" />
<input type="hidden" name="method" value="" />
<input type="hidden" name="isplogin" value="true" />
<input type="submit" id="login" tabindex="4" name="submit" 
class="input-submit large" value="登录" />

解决方案 »

  1.   

    使用校内网的API
    http://dev.xiaonei.com/wiki/%E9%A6%96%E9%A1%B5
      

  2.   

    使用下面的代码也无法实现自动登陆和写日记的功能,
    是否缺少Cookie的相关参数?
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;namespace xiaoneiwangblog
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                //校内网用户名和密码
                username.Text = "[email protected]";
               password.Text = "rfv123";
            }        //在校内网写日志
            private void button1_Click(object sender, EventArgs e)
            {
                string user = username.Text;    //校内网用户名
                string pwd = password.Text;     //校内网密码       
                string blogtitle = title.Text;  //日志标题
                string blogbody = body.Text;    //日志正文            //登陆校内网
                CookieContainer cookies = login(user, pwd);            HttpWebRequest sendreq = (HttpWebRequest)WebRequest.Create("http://blog.xiaonei.com/NewEntry.do");
                byte[] message = Encoding.UTF8.GetBytes("title=" + blogtitle + "&body=" + blogbody + "&categoryid=0&blogControl=99&passwordProtedted=0");
                sendreq.ContentType = "application/x-www-form-urlencoded";
                sendreq.Method = "POST";
                sendreq.ContentLength = message.Length;
                sendreq.CookieContainer = cookies;
                sendreq.GetRequestStream().Write(message, 0, message.Length);
                sendreq.GetRequestStream().Close();
                sendreq.GetResponse();            MessageBox.Show("恭喜您,您的日志已经发表成功,请查看!~");
            }        //登陆校内网
            public CookieContainer login(string username, string password)
            {
                CookieContainer cookies = new CookieContainer();
                HttpWebRequest logreq = (HttpWebRequest)WebRequest.Create("http://login.xiaonei.com/Login.do");
                logreq.CookieContainer = cookies;
                byte[] postdata = Encoding.UTF8.GetBytes("email=" + username + "&password=" + password);
                logreq.Method = "POST";
                logreq.ContentLength = postdata.Length;
                logreq.ContentType = "application/x-www-form-urlencoded";
                logreq.GetRequestStream().Write(postdata, 0, postdata.Length);
                logreq.GetRequestStream().Close();
                logreq.GetResponse();            return cookies;
            }        //清空已填写的用户名、密码以、日志标题和正文
            private void button2_Click(object sender, EventArgs e)
            {
                username.Text = null;
                password.Text = null;
                title.Text = null;
                body.Text = null;
            } 
        }
    }
      

  3.   

    webBrowser1 这个对象有问题,实例化的时候报错了
      

  4.   

    webBrowser1就是直接拖动的,
    webBrowser控件的Name属性
      

  5.   

    你是使用.net framework中System.Windows.Forms.WebBrowser控件么?(而不是mshtml.dll)那些代码大多数需要写到 DocumentCompleted 事件处理中!
      

  6.   

    是 System.Windows.Forms.WebBrowser控件
      

  7.   

    什么事件触发执行DocumentCompleted 方法呢?
      

  8.   

      //下面的代码能自动登陆,但是无法自动发帖写日志 
          private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
               //自动登陆校内网
                webBrowser1.Navigate(http://login.xiaonei.com/Login.do);
                HtmlElement btnSubmit = webBrowser1.Document.All["submit"];
                HtmlElement tbUserid = webBrowser1.Document.All["email"];
                HtmlElement tbPasswd = webBrowser1.Document.All["password"];
                tbUserid.SetAttribute("value", "[email protected]");
                tbPasswd.SetAttribute("value", "rfv123");
                btnSubmit.InvokeMember("click");
                
                //自动发帖写日志
                webBrowser1.Navigate("http://blog.xiaonei.com/NewEntry.do");
                HtmlElement btnSubmit2 = webBrowser1.Document.All["editorFormBtn"];
                HtmlElement tbTitile = webBrowser1.Document.All["title"];
                HtmlElement tbBody = webBrowser1.Document.All["body"];
                tbTitile.SetAttribute("value","testTitle");
                tbBody.SetAttribute("value","testBody");
                btnSubmit2.InvokeMember("click");
            }
    登陆页面的部分Html代码
    label for="email">帐号: </label> 
    <input type="text" name="email" tabindex="1" value="" id="email" class="input-text"><label for="password">密码: </label> 
    <input type="password" id="password" name="password" <input type="submit" id="login" tabindex="4" name="submit" 
    class="input-submit large" value="登录" />
    发帖写日志页面的部分代码
    name="title" id="title"
    id="editorFormBtn" tabindex="4" onclick="EDITOR_ALERT=false" 
    textarea name="body" id="editor
      

  9.   

    发帖是否还要HttpWebRequest ?
      

  10.   

    按照下面的代码,在校内网登陆和发帖,
    提示脚本错误,请大家帮忙看下,谢谢提示错误信息
    行:477
    字符:1
    'window.tinyMCE.activeEditor'为空或不是对象
    代码:0
    URL:http://blog.renren.com/NewEntry.do是否继续在该页面上运行脚本程序?选择确定后,是可以发帖子
            bool isLogin = false; //标记是否登陆        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {           
                if (isLogin == false)
                {
                  //自动登陆校内网
                    isLogin = true;
                    webBrowser1.Navigate("http://login.xiaonei.com/Login.do");
     HtmlElement btnSubmit = webBrowser1.Document.All["submit"];
     HtmlElement tbUserid = webBrowser1.Document.All["email"];
     HtmlElement tbPasswd = webBrowser1.Document.All["password"];
                    tbUserid.SetAttribute("value", "[email protected]");
                    tbPasswd.SetAttribute("value", "rfv123");
                    btnSubmit.InvokeMember("click");
                }            Thread.Sleep(5000); //等待5秒
                
                //自动发帖写日志
                webBrowser1.Navigate("http://blog.xiaonei.com/NewEntry.do");            Thread.Sleep(5000); //等待5秒
    HtmlElement btnSubmit2 = webBrowser1.Document.All["editorFormBtn"];
    HtmlElement tbTitile = webBrowser1.Document.All["title"];
    HtmlElement tbBody = webBrowser1.Document.All["body"];
                tbTitile.SetAttribute("value","testTitle");
                tbBody.SetAttribute("value","testBody");
                btnSubmit2.InvokeMember("click");
                Thread.Sleep(5000); //等待5秒
            }
      

  11.   

    异常提示信息:
    'window.tinyMCE.activeEditor'为空或不是对象