what is in posthtml()?did you save the CookieColelction in a static variable or if web form, Application or Session variable?

解决方案 »

  1.   

    是winform。
    posthtml是一个返回网页的函数。并且也有一个ref /// <summary>
    /// post 一个数据。得到返回结果
    /// </summary>
    /// <param name="Referer">引用页</param>
    /// <param name="url">提交页</param>
    /// <param name="lcPostData">提交的数据</param>
    /// <returns></returns>
    public static string posthtml (ref CookieCollection oCookies ,string Referer,string url ,string lcPostData)
    {
    byte [] lbPostBuffer = System.Text.Encoding.GetEncoding("gb2312").GetBytes(lcPostData);
    HttpWebRequest Reg2Post = (HttpWebRequest) WebRequest.Create(url);
    Reg2Post.Method="POST";
    Reg2Post.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*";
    Reg2Post.Referer = Referer;
    Reg2Post.ContentType = "application/x-www-form-urlencoded";
    //Reg2Post.TransferEncoding = "gzip, deflate";
    Reg2Post.UserAgent = "Mozilla/4.0";
    Reg2Post.ContentLength = lbPostBuffer.Length;
    //Reg2Post.Connection = "Keep-Alive";
    Reg2Post.CookieContainer = new CookieContainer();
    if (oCookies != null && oCookies.Count > 0) 
    {
    Reg2Post.CookieContainer.Add(oCookies);
    }
    Stream loPostData = Reg2Post.GetRequestStream();
    loPostData.Write(lbPostBuffer,0,lbPostBuffer.Length);
    loPostData.Close();
     
    HttpWebResponse MyWebResponse = (HttpWebResponse) Reg2Post.GetResponse(); if (MyWebResponse.Cookies.Count > 0)
    if (oCookies == null)
    {
    oCookies = MyWebResponse.Cookies;
    }
    else 
    {
    // ** If we already have cookies update the list
    foreach (Cookie oRespCookie in MyWebResponse.Cookies) 
    {
    bool bMatch = false;
    foreach(Cookie oReqCookie in oCookies) 
    {
    if (oReqCookie.Name == oRespCookie.Name) 
    {
    oReqCookie.Value = oRespCookie.Name;
    bMatch = true;
    break; // 
    }
    }
    if (!bMatch)
    oCookies.Add(oRespCookie);
    }
    }
    Encoding enc = Encoding.GetEncoding("gb2312");
    StreamReader MyResponseStream = new StreamReader(MyWebResponse.GetResponseStream(),enc);
    string ser = MyResponseStream.ReadToEnd();
    MyWebResponse.Close();
    MyResponseStream.Close();
    return ser;
    }
      

  2.   

    make oCookies a member variable in your Form, or should be using CookieContainer directly?class YourForm : Form
    {
       CookieCollection oCookies = new CookieCollection();
    string html = Login.LoginEach2(ref oCookies,labName.Text,pass);
    see
    http://www.430000.net/printpage.asp?ArticleID=69
      

  3.   

    程序大概出问题的代码如下:
    Form1.cs
    public class Form1 : System.Windows.Forms.Form
    {
    private void button12_Click(object sender, System.EventArgs e)
    {
    CookieCollection oCookies = new CookieCollection();
    string html = Login.LoginEach2(ref oCookies,labName.Text,pass);
    MessageBox.Show(html);//这里显示的是登陆成功的网页
    .....可这里随后的操作却无法在用这个登陆信息了。访问其他页面的时候就提示用户登陆了。好像是没有登陆过一样
    }}Login.cspublic class Login
    {
    public Login()
    {}
    /// <summary>
    /// post 一个数据。得到返回结果
    /// </summary>
    /// <param name="Referer">引用页</param>
    /// <param name="url">提交页</param>
    /// <param name="lcPostData">提交的数据</param>
    /// <returns></returns>
    public static string posthtml (ref CookieCollection oCookies ,string Referer,string url ,string lcPostData)
    {
    byte [] lbPostBuffer = System.Text.Encoding.GetEncoding("gb2312").GetBytes(lcPostData);
    HttpWebRequest Reg2Post = (HttpWebRequest) WebRequest.Create(url);
    Reg2Post.Method="POST";
    Reg2Post.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*";
    Reg2Post.Referer = Referer;
    Reg2Post.ContentType = "application/x-www-form-urlencoded";
    //Reg2Post.TransferEncoding = "gzip, deflate";
    Reg2Post.UserAgent = "Mozilla/4.0";
    Reg2Post.ContentLength = lbPostBuffer.Length;
    //Reg2Post.Connection = "Keep-Alive";
    Reg2Post.CookieContainer = new CookieContainer();
    if (oCookies != null && oCookies.Count > 0) 
    {
    Reg2Post.CookieContainer.Add(oCookies);
    }
    Stream loPostData = Reg2Post.GetRequestStream();
    loPostData.Write(lbPostBuffer,0,lbPostBuffer.Length);
    loPostData.Close();
     
    HttpWebResponse MyWebResponse = (HttpWebResponse) Reg2Post.GetResponse(); if (MyWebResponse.Cookies.Count > 0)
    if (oCookies == null)
    {
    oCookies = MyWebResponse.Cookies;
    }
    else 
    {
    // ** If we already have cookies update the list
    foreach (Cookie oRespCookie in MyWebResponse.Cookies) 
    {
    bool bMatch = false;
    foreach(Cookie oReqCookie in oCookies) 
    {
    if (oReqCookie.Name == oRespCookie.Name) 
    {
    oReqCookie.Value = oRespCookie.Name;
    bMatch = true;
    break; // 
    }
    }
    if (!bMatch)
    oCookies.Add(oRespCookie);
    }
    }
    Encoding enc = Encoding.GetEncoding("gb2312");
    StreamReader MyResponseStream = new StreamReader(MyWebResponse.GetResponseStream(),enc);
    string ser = MyResponseStream.ReadToEnd();
    MyWebResponse.Close();
    MyResponseStream.Close();
    return ser;
    }
    public static string LoginEach2 (CookieCollection oCookies ,string UserName,string Password)
    {
    string postdata = "eachname=" + UserName + "&eachpass=" + Password + "&image.x=21&image.y=7";
    string html = posthtml(ref oCookies,"http://www.xxx.com/xx/big_login.php","http://www.xxx.com/xx/big_login.php",postdata);
    return html;
    }}
      

  4.   

    你的ref格式应该没错!你判断用户是否登录,是根据什么来判断的?
      

  5.   

    其实我是要登陆www.eachnet.com。大家可以试一下。看看到底什么原因。奇怪的不得了。