我现在要模拟登录网站,从网上找许多代码都不好用   有谁做过详细介绍下啊~~~~   

解决方案 »

  1.   

    http://topic.csdn.net/u/20110320/11/0e904c7e-1ae7-4e89-bc3e-26f6e87f8fcc.html
      

  2.   

    基本方法:public static string RequestUrl(string strUrl, 
                                    Dictionary<string, string> postData, 
                                    ref CookieContainer objCookieContainer)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl);
        req.Method = "POST";
        req.KeepAlive = true;
        req.ContentType = "application/x-www-form-urlencoded";
        //req.Timeout = 20;    if (objCookieContainer == null)
            objCookieContainer = new CookieContainer();    req.CookieContainer = objCookieContainer;
            
        req.ContentLength = 0;
        if (postData != null && postData.Count > 0)
        {
            StringBuilder objEncodedPostDatas = new StringBuilder();
            foreach (var kv in postData)
            {
                objEncodedPostDatas.Append(HttpUtility.UrlEncode(kv.Key));
                objEncodedPostDatas.Append("=");
                objEncodedPostDatas.Append(HttpUtility.UrlEncode(kv.Value));
                objEncodedPostDatas.Append("&");
            }
                
            byte[] byteData = Encoding.UTF8.GetBytes(objEncodedPostDatas.ToString().TrimEnd('&'));
            req.ContentLength = byteData.Length;
            using (Stream reqStream = req.GetRequestStream())
            {
                reqStream.Write(byteData, 0, byteData.Length);
            }
        }    var strResponse = "";
        using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
        {
            objCookieContainer = req.CookieContainer;
            using (Stream resStream = res.GetResponseStream())
            {
                using (StreamReader sr = new StreamReader(resStream, Encoding.UTF8))
                {
                    strResponse = sr.ReadToEnd();
                }
            }
        }
        return strResponse;
    }调用:string uriStr = "http://xxxx/login.aspx";
    string strId = "用户名";
    string strPassword = "密码";var postData = new Dictionary<string, string>();
    postData.Add("控件ID1", strId);
    postData.Add("控件ID2", strPassword);
    CookieContainer cc = new CookieContainer();
    var response = RequestUrl(uriStr, postData, ref cc);
      

  3.   

    fangxinggood您好:
            string uriStr = "http://my.chinahr.com/login.aspx?ReturnUrl=%2findex.aspx";
            string strId = "[email protected]";
            string strPassword = "123456abc";        Dictionary<string, string> postData = new Dictionary<string, string>();
            postData.Add("LoginModule_ascx$tbUserName", strId);
            postData.Add("LoginModule_ascx$tbPassword", strPassword);
            CookieContainer cc = new CookieContainer();
            string response = RequestUrl(uriStr, postData, ref cc);
            Response.Write(response);上面是我调用写的代码,最后还是停留在登录页,帮忙看看啊
      

  4.   

    再加一个方法,用于GET
    public static string RequestUrl(string strUrl, ref CookieContainer objCookieContainer)
    {
        HttpWebRequest req = (HttpWebRequest)WebRequest.Create(strUrl);
        req.Method = "GET";
        req.CookieContainer = objCookieContainer;
        using (HttpWebResponse res = (HttpWebResponse)req.GetResponse())
        {
            using (Stream resStream = res.GetResponseStream())
            {
                objCookieContainer = req.CookieContainer;
                using (StreamReader sr = new StreamReader(resStream, Encoding.UTF8))
                {
                    return sr.ReadToEnd();
                }
            }
        }
    }
      

  5.   

    调用:
    string url = "http://my.chinahr.com/login.aspx?ReturnUrl=%2findex.aspx";
    CookieContainer cc = new CookieContainer();
    // 第一次GET请求,获取cookie
    RequestUrl(url, ref cc);string strId = "[email protected]";
    string strPassword = "123456abc";
    Dictionary<string, string> postData = new Dictionary<string, string>();
    postData.Add("LoginModule_ascx$tbUserName", strId);
    postData.Add("LoginModule_ascx$tbPassword", strPassword);
    postData.Add("LoginModule_ascx$btnLogin", "登录");string response = RequestUrl(url, postData, ref cc);
    if (response.Contains("正在处理中,请您稍等..."))
        Console.WriteLine("login success");string urlIndex = "http://my.chinahr.com/index.aspx";
    response = RequestUrl(urlIndex, ref cc);
      

  6.   

    fangxinggood 非常感谢!!!
      

  7.   

    登陆之后 如果点击退出按钮 那么 再登陆的时候 就 不能跳转了 就能到 登陆页 是不是 要给 Cookie 设定时间!