网页用HttpWebRequest实现自动登录后,再跳转用IE打开登录后的网页如何实现?求各位高手帮忙,救命啊

解决方案 »

  1.   

    网页用HttpWebRequest实现自动登录后,再跳转用IE打开登录后的网页如何实现?求各位高手帮忙,救命啊 
      

  2.   

    建议你直接使用.net framework提供的WebBrowser控件,采用HtmlElent实现自动登录:
    有了WebBrowser类,终于不用自己手动封装SHDocVw的 AxWebBrowser这个ActiveX控件了。这个类如果仅仅作为一个和IE一模一样浏览器,那就太没意思了(还不如直接用IE呢)。那么,无论我们是想做一个“定制版IE”,还是希望利用HTML来做用户界面(指WinApp而非WebApp。许多单机软件,包括Windows的帮助支持中心,都是HTML做的),都少不了Windows Form和包含在WebBrowser中的Web页面的交互。本文将通过几个实际的例子,初步介绍一下WinForm和WebBrowser所包含的 Web页面之间的交互。下面的代码假设你已经建立了一个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。
    HtmlElement formLogin = webBrowser.Document.Forms["loginForm"];
    //……
    formLogin.InvokeMember("submit");
    ※关于表单的提交,的确还有另一种方法就是获取form元素而不是button,并用form元素的 submit方法:本文之所以没有推荐这种方法,是因为现在的网页,很多都在submit按钮上添加onclick事件,以对提交的内容做最基本的验证。如果直接使用form的submit方法,这些验证代码就得不到执行,有可能会引起错误。
      

  3.   

    HttpWebRequest结合webbrower
    CookieContainer myCookieContainer = new CookieContainer();  
    string cookieStr = webBrowser1.Document.Cookie;
    string[] cookstr = cookieStr.Split( '; ');  
    foreach (string str in cookstr)  
    {  }  
    HttpWebRequest hreq=(HttpWebRequest)HttpWebRequest.Create("");  
    hreq.Method= "POST ";  
    hreq.ContentType= "application/x-www-form-urlencoded ";  
    hreq.CookieContainer= myCookieContainer; 
      

  4.   

    这个很复杂,关键的一点是要保存登陆之后产生的cookie。3楼的代码
    应该是可用的。但具体实施可能还需要更多的语句。
      

  5.   

    不好意思,应该是 CookieContainer MycookieContainer= new CookieContainer(); 
    HttpWebRequest request = WebRequest.Create(URI) as HttpWebRequest;
    request.Method = "GET";
    request.KeepAlive = false;
    request.CookieContainer = MycookieContainer;
      

  6.   

    说有都是用winform实现的,我要的是用WEB实现的
      

  7.   

     http://blog.renren.com/blog/bp/QgX5xgb2lH
      

  8.   

    或许是我描述的不清楚,我的意思是在网页下实现自动登录其它网页后,再打开其他网页的页面
      例如:
     private static CookieContainer cc = new CookieContainer();
    string strHtml = GetHtml("http://192.12.10.3:8056/Login.aspx", "yhdh=admin&mm=000000", true, cc);
    //strHtml 实现登录,返回的HTML信息,
            if (strHtml.IndexOf("***网页系统") == -1)// 如果存在"***网页系统"表示登录成功
            {
                
             //打开"***网页系统下的aaa.aspx页面"  这样做不行  要怎样才可实现????求高手回答      
              Response.Redirect("aaa.aspx",false);
            } public static string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)
        {
            if (string.IsNullOrEmpty(postData))
            {
                return GetHtml(url, cookieContainer);
            }        Thread.Sleep(delay);//等待
            currentTry++;        HttpWebRequest httpWebRequest = null;
            HttpWebResponse httpWebResponse = null;
            try
            {
                byte[] byteRequest = Encoding.Default.GetBytes(postData);            httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(url);
                httpWebRequest.CookieContainer = cookieContainer;
                httpWebRequest.ContentType = contentType;
                httpWebRequest.ServicePoint.ConnectionLimit = maxTry;
                httpWebRequest.Referer = url;
                httpWebRequest.Accept = accept;
                httpWebRequest.UserAgent = userAgent;
                httpWebRequest.Method = isPost ? "POST" : "GET";
                httpWebRequest.ContentLength = byteRequest.Length;            Stream stream = httpWebRequest.GetRequestStream();
                stream.Write(byteRequest, 0, byteRequest.Length);
                stream.Close();            httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                Stream responseStream = httpWebResponse.GetResponseStream();
                StreamReader streamReader = new StreamReader(responseStream, encoding);
                string html = streamReader.ReadToEnd();
                streamReader.Close();
                responseStream.Close();
                currentTry = 0;            httpWebRequest.Abort();
                httpWebResponse.Close();            return html;
            }
            catch (Exception e)
            {
                //Console.ForegroundColor = ConsoleColor.Red;
                //Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
                //Console.ForegroundColor = ConsoleColor.White;            if (currentTry <= maxTry)
                {
                    GetHtml(url, postData, isPost, cookieContainer);
                }
                //currentTry--;            if (httpWebRequest != null)
                {
                    httpWebRequest.Abort();
                } if (httpWebResponse != null)
                {
                    httpWebResponse.Close();
                }
                return string.Empty;
            }
        }