站外登陆的实现(BS版的无忧登陆),不支持验证码
2010-11-12 08:15:29 来源:WEB开发网核心提示:第二种方法 利用mshtml命名空间实现表单赋值,具体实现如下:using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.
第二种方法 利用mshtml命名空间实现表单赋值。
具体实现如下:
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Diagnostics; using System.IO; using SHDocVw; using System.Text; using System.Net; using mshtml; namespace WebApplication2 { public partial class login2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { string URI = "***"; var ie = new InternetExplorer(); object vPost, vHeaders, vFlags, vTargetFrame; vPost = null; vFlags = null; vTargetFrame = null; vHeaders = "Content-Type: application/x-www-form-urlencoded" + Convert.ToChar(10) + Convert.ToChar(13); ie.Visible = true; ie.Navigate(URI, ref vFlags, ref vTargetFrame, ref vPost, ref vHeaders); try { System.Threading.Thread.Sleep(1000); HTMLDocument doc = (HTMLDocument)ie.Document; HTMLInputElement keyword = (HTMLInputElement)doc.getElementById("tbUserName"); keyword.value = "***"; HTMLInputElement keyword1 = (HTMLInputElement)doc.getElementById("tbPassword"); keyword1.value = "***"; HTMLInputElement submit = (HTMLInputElement)doc.getElementById("btnLogin"); submit.click(); } catch (Exception) { return; } } } }
第三种方法 是自己调用底层dll 来实现打开IE,并对表单赋值。调用的类为I ELoginHelper,
客户端实现如下:
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using IELoginHelper; namespace WebApplication2 { public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { IEHelper ie = new IEHelper(); ie.OpenAVisibleBlankDocument(); object p = null; string url = @"***"; bool ret = ie.Navigate(url, ref p, ref p, ref p, ref p); ie.SetValueById("txtusername", "***"); ie.SetValueById("txtpwd", "***"); ie.ClickButtonByName("***"); } } }
第一种第二种如果客户端页面JS出错,Button按钮Type不是Submit按钮等可能会出异常。
更多精彩
赞助商链接