none
winform 中如何模拟登陆 并购物下单 RRS feed

  • 问题

  • 您好:

       我想在winform中实现模拟登陆 表单是这样的

    <form name="login" action="http://localhost/index.php?main_page=login&amp;action=process" method="post" id="loginForm"><input type="hidden" name="securityToken" value="7adb726eb7934332ebc39408d455d2b9">


    <input type="text" name="email_address" size="41" maxlength="96" id="login-email-address"><br class="clearBoth">

    <input type="password" name="password" size="41" maxlength="40" id="login-password"><br class="clearBoth">
    <input type="hidden" name="securityToken" value="7adb726eb7934332ebc39408d455d2b9"></fieldset>

    <div class="buttonRow forward"><input type="image" src="includes/templates/classic/buttons/english/button_login.gif" ></div>
    </form>

    希望实现能模拟登陆,而且能模拟登陆后的操作 比如下单或者获取登陆后的页面内容。 希望针对这个表单给出比较详细的代码, 非常感谢!!!

    2014年4月29日 0:35

答案

  • 你好:

    使用WebBrowser,WebClient也可以实现同样功能.

    Windows Forms 自带的WebBrowser控件可以加载一个网页的URL,一个本地的htm文件,或者直接加载HTML字符串内容,通过其Document属性可以获取到网页的HTML内容,进而可以获取到网页中各个表单的元素对象,可以对这些对象赋值。比如在一个文本框中填写一些字符串,调用一个按钮的点击事件,让一个表单提交等。

    实例代码:

    webBrowser1.Document.GetElementById("id").InnerText =txtuser.Text.ToString().Trim();
    
    webBrowser1.Document.GetElementById("passwd").InnerText =txtpwd.Text.ToString().Trim();
    
    HtmlElement formLogin = webBrowser1.Document.Forms["frmLogin"];
    
    formLogin.InvokeMember("submit");

    WebClient模拟发送请求的代码参考如下:

            public string Get(string Url,string Referer, Encoding Encoder,ref string CookieStr)
            {
                string result = "";
    
                WebClient myClient = new WebClient();
                myClient.Headers.Add("Accept: */*");
                myClient.Headers.Add("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET4.0E; .NET4.0C; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; SE 2.X MetaSr 1.0)");
                myClient.Headers.Add("Accept-Language: zh-cn");
                myClient.Headers.Add("Content-Type: multipart/form-data");
                myClient.Headers.Add("Accept-Encoding: gzip, deflate");
                myClient.Headers.Add("Cache-Control: no-cache");
                if (CookieStr != "")
                {
                    myClient.Headers.Add(CookieStr);
                }
                myClient.Encoding = Encoder;
                result = myClient.DownloadString(Url);
                if (CookieStr == "")
                {
                    CookieStr = myClient.ResponseHeaders["Set-Cookie"].ToString();
                    CookieStr = GetCookie(CookieStr);
                }
                return result;
            }
            public string Post(string Url, string Referer, Encoding Encoder, ref string CookieStr, string Data)
            {
                string result = "";
    
                WebClient myClient = new WebClient();
                myClient.Headers.Add("Accept: */*");
                myClient.Headers.Add("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET4.0E; .NET4.0C; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; SE 2.X MetaSr 1.0)");
                myClient.Headers.Add("Accept-Language: zh-cn");
                myClient.Headers.Add("Content-Type: multipart/form-data");
                myClient.Headers.Add("Accept-Encoding: gzip, deflate");
                myClient.Headers.Add("Cache-Control: no-cache");
                if (CookieStr != "")
                {
                    myClient.Headers.Add(CookieStr);
                }
                myClient.Encoding = Encoder;
                result = myClient.UploadString(Url, Data);
                if (CookieStr == "")
                {
                    CookieStr = myClient.ResponseHeaders["Set-Cookie"].ToString();
                    CookieStr = GetCookie(CookieStr);
                }
                return result;
            }
            private string GetCookie(string CookieStr)
            {
                string result = "";
    
                string[] myArray = CookieStr.Split(',');
                if (myArray.Count() > 0)
                {
                    result = "Cookie: ";
                    foreach (var str in myArray)
                    {
                        string[] CookieArray = str.Split(';');
                        result += CookieArray[0].Trim();
                        result += "; ";
                    }
                    result = result.Substring(0, result.Length - 2);
                }
                return result;
            }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2014年4月30日 2:30
    版主

全部回复

  • 你用fiddler2查看ie发送的数据,然后用httpwebrequest发送一样的

    http://feiyun0112.cnblogs.com/

    2014年4月30日 1:56
    版主
  • 你好:

    使用WebBrowser,WebClient也可以实现同样功能.

    Windows Forms 自带的WebBrowser控件可以加载一个网页的URL,一个本地的htm文件,或者直接加载HTML字符串内容,通过其Document属性可以获取到网页的HTML内容,进而可以获取到网页中各个表单的元素对象,可以对这些对象赋值。比如在一个文本框中填写一些字符串,调用一个按钮的点击事件,让一个表单提交等。

    实例代码:

    webBrowser1.Document.GetElementById("id").InnerText =txtuser.Text.ToString().Trim();
    
    webBrowser1.Document.GetElementById("passwd").InnerText =txtpwd.Text.ToString().Trim();
    
    HtmlElement formLogin = webBrowser1.Document.Forms["frmLogin"];
    
    formLogin.InvokeMember("submit");

    WebClient模拟发送请求的代码参考如下:

            public string Get(string Url,string Referer, Encoding Encoder,ref string CookieStr)
            {
                string result = "";
    
                WebClient myClient = new WebClient();
                myClient.Headers.Add("Accept: */*");
                myClient.Headers.Add("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET4.0E; .NET4.0C; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; SE 2.X MetaSr 1.0)");
                myClient.Headers.Add("Accept-Language: zh-cn");
                myClient.Headers.Add("Content-Type: multipart/form-data");
                myClient.Headers.Add("Accept-Encoding: gzip, deflate");
                myClient.Headers.Add("Cache-Control: no-cache");
                if (CookieStr != "")
                {
                    myClient.Headers.Add(CookieStr);
                }
                myClient.Encoding = Encoder;
                result = myClient.DownloadString(Url);
                if (CookieStr == "")
                {
                    CookieStr = myClient.ResponseHeaders["Set-Cookie"].ToString();
                    CookieStr = GetCookie(CookieStr);
                }
                return result;
            }
            public string Post(string Url, string Referer, Encoding Encoder, ref string CookieStr, string Data)
            {
                string result = "";
    
                WebClient myClient = new WebClient();
                myClient.Headers.Add("Accept: */*");
                myClient.Headers.Add("User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET4.0E; .NET4.0C; InfoPath.2; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; SE 2.X MetaSr 1.0)");
                myClient.Headers.Add("Accept-Language: zh-cn");
                myClient.Headers.Add("Content-Type: multipart/form-data");
                myClient.Headers.Add("Accept-Encoding: gzip, deflate");
                myClient.Headers.Add("Cache-Control: no-cache");
                if (CookieStr != "")
                {
                    myClient.Headers.Add(CookieStr);
                }
                myClient.Encoding = Encoder;
                result = myClient.UploadString(Url, Data);
                if (CookieStr == "")
                {
                    CookieStr = myClient.ResponseHeaders["Set-Cookie"].ToString();
                    CookieStr = GetCookie(CookieStr);
                }
                return result;
            }
            private string GetCookie(string CookieStr)
            {
                string result = "";
    
                string[] myArray = CookieStr.Split(',');
                if (myArray.Count() > 0)
                {
                    result = "Cookie: ";
                    foreach (var str in myArray)
                    {
                        string[] CookieArray = str.Split(';');
                        result += CookieArray[0].Trim();
                        result += "; ";
                    }
                    result = result.Substring(0, result.Length - 2);
                }
                return result;
            }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2014年4月30日 2:30
    版主
  • 感谢大家的帮忙,

      我的代码忘记把cookie传递过去了,所以一直登陆不了,谢谢!

    2014年5月2日 0:44