如题,
winform做来2个文本框(账号和密码),填写过后点登录按钮相当于在网站上填写的表单,传到登录处理页面处理,如何登录?怎么做?

解决方案 »

  1.   

    用webbrowser控件 System.Windows.Forms.HtmlDocument HTMLDocument = wbHistorySearch.Document;
                System.Windows.Forms.HtmlElement loginName= HTMLDocument.GetElementById("loginName");
                loginName.InnerText = System.Configuration.ConfigurationSettings.AppSettings["LoginName"].ToString();
                //loginName.Enabled = false;
                System.Windows.Forms.HtmlElement password = HTMLDocument.GetElementById("passwd");
                password.InnerText = System.Configuration.ConfigurationSettings.AppSettings["Password"].ToString();
                //password.Enabled = false;
                System.Windows.Forms.HtmlElement btnLogin = HTMLDocument.GetElementById("login");
                btnLogin.InvokeMember("click");
                btnLogin.Enabled = false;
                System.Windows.Forms.HtmlElement btnReset = HTMLDocument.GetElementById("Submit2");
                btnReset.Enabled = false;
      

  2.   

    我的思路是可以登录判断用户名和密码,正确之后:Process.Start("地址");打开那个网站。
    不知道可行不可行。。
      

  3.   

    登录方法
    1.用webbrowser控件登录,记录cookies值,访问处理页面时传cookies过去就可以了
    2.用你现在说的用户名和密码登录,但是要处理有验证码的情况!登录前在全局声明一个CookieContainer,以后操作页面把这个CookieContainer实例传过去
      

  4.   


           /// <summary>
           /// 获得验证码
           /// </summary>
            /// <param name="url">验证码地址</param>
           /// <param name="encoding">编码</param>
           /// <param name="myCookie">cookie</param>
           /// <returns></returns>
            public System.Drawing.Image GetImg(string url, string encoding,CookieContainer myCookie)
            {  
                System.Drawing.Image img = null;
                HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);   
                myRequest.Credentials = CredentialCache.DefaultCredentials;
                myRequest.CookieContainer = myCookie;
                myRequest.Accept = "*/*";   
                myRequest.Method = "GET";
                myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6; .NET CLR 2.0.50727; CIBA)";
                myRequest.ContentType = "application/x-www-form-urlencoded";
                myRequest.KeepAlive = true;
                myRequest.UseDefaultCredentials = true;   
              
                HttpWebResponse response = (HttpWebResponse)myRequest.GetResponse();  
                Stream responseStream = response.GetResponseStream();
                img = System.Drawing.Image.FromStream(responseStream);
                StreamReader readStream = new StreamReader(responseStream, System.Text.Encoding.GetEncoding(encoding));                
                string pagecheckstring = readStream.ReadToEnd();
                return img;
            }
      

  5.   

    这个很简单,使用winform中的webbrowser控件,具体如下:下面的代码假设你已经建立了一个Windows Form,上面有一个WebBrowser名为“webBrowser”。Study Case 1:用WinForm的Event Handler响应Web页面的事件现在有这样一个Windows Application,它的界面上只有一个WebBrowser,显示一个本地的HTML文件作为界面。现在的问题是,所有逻辑都可以放在HTML文件里,唯独“关闭”按钮遇到了困难——通常,Web页面是没有办法直接控制浏览器的,更不用说结束这个WinForm程序了。但是,在.Net 2.0当中,“由Windows Form响应Web页面的事件”已经成为了现实。在.Net 2.0中,整个HTML文档以及其包含的各个HTML元素,都和一个个HtmlDocument、HtmlElement之类的.Net对象对应。因此只要找到这个“关闭”按钮对应的HtmlElement对象,为其click事件添加Event Handler即可。
    假设HTML源代码如下:
    <html>
    <body>
    <input type="button" id="btnClose" value="关闭" />
    </body>
    </html>那么找出该按钮并为之添加Event Handler的代码如下:HtmlDocument htmlDoc = webBrowser.Document;
    HtmlElement btnElement = htmlDoc.All["btnClose"];
    if (btnElement != null)
    {
    btnElement.click += new HtmlElementEventHandler(HtmlBtnClose_Click);
    }其中 HtmlBtnClose_Click是按下Web按钮时的Event Handler。很简单吧?那么稍稍高级一点的——我们都知道一个HTML元素可能有很多各种各样的事件,而HtmlElement这个类只给出最常用、共通的几个。那么,如何响应其他事件呢?这也很简单,只需要调用HtmlElement的AttachEventHandler就可以了:btnElement.AttachEventHandler("onclick", new EventHandler(HtmlBtnClose_Click));
    //这一句等价于上面的 btnElement.click += new HtmlElementEventHandler(HtmlBtnClose_Click);对于其他事件,把"onclick"换成该事件的名字就可以了。例如:
    formElement.AttachEventHandler("onsubmit", new EventHandler(HtmlForm_Submit));Study Case 2:表单(form)的自动填写和提交
    要使我们的WebBrowser具有自动填表、甚至自动提交的功能,并不困难。假设有一个最简单的登录页面,输入用户名密码,点“登录”按钮即可登录。已知用户名输入框的id(或Name,下同)是username,密码输入框的id是password,“登录”按钮的id是submitbutton,那么我们只需要在webBrowser的 DocumentCompleted事件中使用下面的代码即可:HtmlElement btnSubmit = webBrowser.Document.All["submitbutton"];
    HtmlElement tbUserid = webBrowser.Document.All["username"];
    HtmlElement tbPasswd = webBrowser.Document.All["password"];
    if (tbUserid == null || tbPasswd == null || btnSubmit == null)
        return;
    tbUserid.SetAttribute("value", "smalldust");
    tbPasswd.SetAttribute("value", "12345678");
    btnSubmit.InvokeMember("click");这里我们用SetAttribute来设置文本框的“value”属性,用InvokeMember来调用了按钮的“click”方法。因为不同的Html元素,其拥有的属性和方法也不尽相同,所以.Net 2.0提供了统一的HtmlElement来概括各种Html元素的同时,提供了这两个方法以调用元素特有的功能。关于各种Html元素的属性和方法一览,可以查阅MSDN的DHTML Reference。※关于表单的提交,的确还有另一种方法就是获取form元素而不是button,并用form元素的 submit方法:HtmlElement formLogin = webBrowser.Document.Forms["loginForm"];
    //……
    formLogin.InvokeMember("submit");本文之所以没有推荐这种方法,是因为现在的网页,很多都在submit按钮上添加onclick事件,以对提交的内容做最基本的验证。如果直接使用form的submit方法,这些验证代码就得不到执行,有可能会引起错误。
      

  6.   


    可以写个完整的么???
    比如用winform登录CSDN?
      

  7.   


    我用的是 WebHttpRequest 和 WebHttpResponse获取cookie,然后再传到WebBrowser里
    这样就显得直接登录了网站的感觉至于代码,还真不敢给你
      

  8.   

    system.Net.HttpWebRequest
    HttpWebRequest.Method = "POST";//POST请求
      

  9.   

    基于问题,我可以给出两种方案:
    1. 用C#创建WinForm工程,添加2个文本框1个按钮,再添加WebBrowser控件,内嵌CSDN网站,点击按钮达到自动登录效果,代码在最下面
    2. 用C#创建WinForm工程,添加2个文本框1个按钮,点击按钮弹出一个IE浏览器,并且转向CSDN网站,然后自动登录。这种比较难,代码很复杂,如果楼主需要,我可以把代码发给你。
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            using (Form f = new Form())
            {
                // Form
                f.Width = 800;
                f.Height = 600;
                f.Text = "自动登录CSDN";
                f.MaximizeBox = false;            // Controls
                f.Controls.Add(new Label()
                {
                    Left = 10,
                    Top = 10,
                    Width = 80,
                    Text = "用户名"
                });
                f.Controls.Add(new TextBox()
                {
                    Left = 100,
                    Top = 10,
                    Width = 680,
                    Name = "uid"
                });
                f.Controls.Add(new Label()
                {
                    Left = 10,
                    Top = 35,
                    Width = 80,
                    Text = "密  码"
                });
                f.Controls.Add(new TextBox()
                {
                    Left = 100,
                    Top = 35,
                    Width = 680,
                    PasswordChar = '*',
                    Name = "pwd"
                });
                f.Controls.Add(new Button()
                {
                    Left = 10,
                    Top = 60,
                    Width = 770,
                    Text = "点击登录",
                    Name = "go"
                });
                f.Controls.Add(new WebBrowser()
                {
                    Left = 10,
                    Top = 90,
                    Width = 770,
                    Height = 500,
                    Url = new Uri("http://passport.csdn.net/UserLogin.aspx"),
                    Name = "csdn"
                });            // Events
                (f.Controls.Find("go", false).First() as Button).Click += (s, e) =>
                {
                    // 省略一些校验代码
                    // ...
                    var uid = (f.Controls.Find("uid", false).First() as TextBox).Text;
                    var pwd = (f.Controls.Find("pwd", false).First() as TextBox).Text;
                    var csdn = (f.Controls.Find("csdn", false).First() as WebBrowser);
                    // 设置用户名
                    csdn.Document.GetElementById("ctl00_CPH_Content_tb_LoginNameOrLoginEmail").SetAttribute("value", uid);
                    // 设置密码
                    csdn.Document.GetElementById("ctl00_CPH_Content_tb_Password").SetAttribute("value", pwd);
                    // 点击登录
                    csdn.Document.GetElementById("ctl00_CPH_Content_Image_Login").RaiseEvent("onclick");
                    // 注意:登录不会成功,因为验证码没有填写!
                };            f.ShowDialog();
            }
        }}
      

  10.   

    其实不用webbrowser控件也行 模拟表单就可以登录了  用webclient类