此问题困扰两天了,郁闷得很,直接贴代码吧:
 public CookieContainer Get_Login()
        {
            CookieContainer cc = new CookieContainer();
            string FormURL = "http://localhost:1070/WebSite3/Login.aspx";                //处理表单的绝对URL地址
            string FormData = "UserName=wq&Password=1";    //表单需要提交的参数,注意改为你已注册的信息。
            byte[] data = Encoding.UTF8.GetBytes(FormData);            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(FormURL);
            request.Method = "POST";    //数据提交方式
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = data.Length;
            request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; TencentTraveler 4.0; .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; CIBA)";
            //模拟一个UserAgent
            Stream newStream = request.GetRequestStream();
            newStream.Write(data, 0, data.Length);            newStream.Close();            request.CookieContainer = cc;            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            cc.Add(response.Cookies);
            Stream stream = response.GetResponseStream();
            string WebContent = new StreamReader(stream, System.Text.Encoding.UTF8).ReadToEnd();
            return cc;
        }private void btnSubmit_Click(object sender, EventArgs e)
        {
            CookieContainer cc = new CookieContainer();
            cc = Get_Login();            //获取登录Cookies            string PhotoClassURL = "http://localhost:1070/WebSite3/Default.aspx";
            HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(PhotoClassURL);
            Myrequest.CookieContainer = cc;
            HttpWebResponse Myresponse = (HttpWebResponse)Myrequest.GetResponse();
            cc.Add(Myresponse.Cookies);
            Stream Mystream = Myresponse.GetResponseStream();
            string sHtml = new StreamReader(Mystream, System.Text.Encoding.UTF8).ReadToEnd();
            txtSource.Text = sHtml;
        }
cc = Get_Login(); 获取的cook数量总是为0,也就是没获取到不知道怎么回事   在Default.aspx页面中代码如下:
protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["UserName"] == null)
        {
            Response.Redirect("Login.aspx");
            return;
        }        if ((string)Session["UserName"] == string.Empty)
        {
            Response.Redirect("Login.aspx");
        }
    }
在Default.aspx页面中代码如下:
protected void btnSumbmit_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection("Data Source=.;database=LifeList;uid=sa;pwd=852527");
        SqlCommand cmd =
            new SqlCommand("select count(*) from tblUser where UserName='" + txtUserName.Text + "' and Password='" +
                           txtPassword.Text + "'", conn);
        conn.Open();
        if (Convert.ToInt32(cmd.ExecuteScalar()) == 1)
        {
            Session["UserName"] = "wq";
            Response.Redirect("Default.aspx");
        }
    }
      
请教达人为什么取不到default.aspx的具体内容啊,登陆获取cookie那段是不是有问题,头都大了,谢谢  

解决方案 »

  1.   

    c# 网页登陆?
    可以考虑用webservice啊,实现BS和CS的数据交互。
      

  2.   

    不想用webserviece,就是想通过post方式实现
      

  3.   

    你看到你把信息写到Cookie里去啊,哪段代码?
      

  4.   

    通过public CookieContainer Get_Login()这个方法啊获取cookie
      

  5.   

    Get_Login()这个方法是在客户端,你服务器上的代码有写入Cookie吗?如果服务器没有写入,客户端当然没有接收到了
      

  6.   

    呃确实是这么回事,没写
    那要怎么做才可以突破这个登录,获取default.aspx源码?
    if (Session["UserName"] == null)
            {
                Response.Redirect("Login.aspx");
                return;
            }
    是这个造成的吧,请教
      

  7.   

    通过抓包工具可以等到如下信息:
    GET /WebSite3/Default.aspx HTTP/1.1
    Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, */*
    Referer: http://localhost:1070/WebSite3/Login.aspx
    Accept-Language: zh-cn
    User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; TencentTraveler 4.0; .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; CIBA)
    Accept-Encoding: gzip, deflate
    Host: localhost:1070
    Connection: Keep-Alive
    Cache-Control: no-cache
    Cookie: COOKIE=Language=2052; ASP.NET_SessionId=1m2s2tf0olvzs445v0h3fq45
    为什么我获取不到这个cookie咧?
      

  8.   

    按道理是不是我获取到了
    Cookie: COOKIE=Language=2052; ASP.NET_SessionId=1m2s2tf0olvzs445v0h3fq45,然后CookieContainer cc = new CookieContainer();
                cc = Get_Login();            //获取登录Cookies            string PhotoClassURL = "http://localhost:1070/WebSite3/Default.aspx";
                HttpWebRequest Myrequest = (HttpWebRequest)WebRequest.Create(PhotoClassURL);
                Myrequest.CookieContainer = cc;
    访问defalut.aspx页面时就可以得到default.aspx页面的源码?
      

  9.   

     public string Post(string url, string strPost, RequestPPT rePpt, string coding)//string user, string password, string checkcode
            {
                string outHtml = "";
                try
                {
                    byte[] data = Encoding.Default.GetBytes(strPost);
                    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                    myRequest.Method = rePpt.Method;
                    myRequest.ContentType = rePpt.ContentType;
                    myRequest.CookieContainer = CookieCtainer;
                    myRequest.Referer = rePpt.Referer;
                    myRequest.ContentLength = data.Length;
                    Stream newStream = myRequest.GetRequestStream();
                    // Send the data.   
                    newStream.Write(data, 0, data.Length);
                    newStream.Close();                // Get response   
                    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
                    myResponse.Cookies = CookieCtion;
                    StreamReader reader;
                    if(coding == "UTF8")
                        reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8);
                    else
                        reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
                    outHtml = reader.ReadToEnd();
                    return outHtml;
                }
                catch (Exception ee)
                {
                    outHtml = ee.Message;
                    return outHtml;
                    // MessageBox.Show(ee.Message);return outHtml;
                }
            }
                rppt.Method = "POST";
                rppt.Referer = "http://www.paynewline.com/";
                rppt.Accept = " image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
                rppt.ContentType = "application/x-www-form-urlencoded";
                rppt.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)";
                html = httpwebclient.Post("http://www.paynewline.com/business/sh_login.do", "dlip=&showmsg=null&dlfs=2&dlm=" + username + "&password=" + password + "&dynamicPwd=&imgnum=" + image, rppt, "UTF8");
              
      

  10.   

    获得cookies
    public void SetCookieContainer(CookieCollection CookieCC)
            {
                CookieCtainer.Add(CookieCC);
            }
            public void SetCookieCollection(CookieCollection CookieCC)
            {
                CookieCtion=CookieCC;
            }public string Post(string url, string strPost, RequestPPT rePpt, string coding)//string user, string password, string checkcode 
            { 
                string outHtml = ""; 
                try 
                { 
                    byte[] data = Encoding.Default.GetBytes(strPost); 
                    HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url); 
                    myRequest.Method = rePpt.Method; 
                    myRequest.ContentType = rePpt.ContentType; 
                    myRequest.CookieContainer = CookieCtainer; 
                    myRequest.Referer = rePpt.Referer; 
                    myRequest.ContentLength = data.Length; 
                    Stream newStream = myRequest.GetRequestStream(); 
                    // Send the data.  
                    newStream.Write(data, 0, data.Length); 
                    newStream.Close();                 // Get response  
                    HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse(); 
                    myResponse.Cookies = CookieCtion; 
                    StreamReader reader; 
                    if(coding == "UTF8") 
                        reader = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8); 
                    else 
                        reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default); 
                    outHtml = reader.ReadToEnd(); 
                    return outHtml; 
                } 
                catch (Exception ee) 
                { 
                    outHtml = ee.Message; 
                    return outHtml; 
                    // MessageBox.Show(ee.Message);return outHtml; 
                } 
            }  public string GetCookieContainer(string url)//string user, string password, string checkcode
            {
                string outHtml = "";            try
                {
                    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
                    req.Method = "GET";
                    req.CookieContainer = CookieCtainer;
                    HttpWebResponse wr = (HttpWebResponse)req.GetResponse();                StreamReader readwr = new StreamReader(wr.GetResponseStream(), Encoding.Default);
                    outHtml = readwr.ReadToEnd();
                    SetCookieCollection(wr.Cookies);
                    SetCookieContainer(wr.Cookies);
                    return outHtml;
                }
                catch (Exception ee)
                {
                    outHtml = ee.Message;
                    return outHtml;
                }
            }httpwebclient.GetCookieContainer("http://www.paynewline.com/");这样先得到COOKIES
                         rppt.Method = "POST";
                rppt.Referer = "http://www.paynewline.com/";
                rppt.Accept = " image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-shockwave-flash, */*";
                rppt.ContentType = "application/x-www-form-urlencoded";
                rppt.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727)";
                html = httpwebclient.Post("http://www.paynewline.com/business/sh_login.do", "dlip=&showmsg=null&dlfs=2&dlm=" + username + "&password=" + password + "&dynamicPwd=&imgnum=" + image, rppt, "UTF8");
           
      

  11.   

    听了一个前辈的方法,问题解决了,
    string formatString ="__VIEWSTATE={0}&txtUsername={1}&txtPassword={2}&btnSubmit={3}&__EVENTVALIDATION={4}";
    string postString = string.Format(formatString, viewState, txtUsername, txtPassword,
     btnSubmit, eventValidation);
    但还是有点不明白,为什么一定要__VIEWSTATE,__EVENTVALIDATION等等,请大虾指教
      

  12.   

    我找到一个很好的http抓包工具叫http watch,很好用,能够把post所需数据一一捕获,很好