none
如何实现自动登入网站? RRS feed

  • 问题

  •  这是网络上找到的原代码,可是好象不能登入进去。请帮忙看看
    string url = "http://b2b.hc360.com/rewrite-url/www/dl.html";

                string indata = "LoginID=mytestcs&Passwd=aabbccdd";


                string outdata = "";

                CookieContainer myCookieContainer = new CookieContainer();
                //新建一个CookieContainer来存放Cookie集合
                HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(url);
                //新建一个HttpWebRequest
                myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
                myHttpWebRequest.ContentLength = indata.Length;
                myHttpWebRequest.Method = "POST";
                myHttpWebRequest.CookieContainer = myCookieContainer;
                //设置HttpWebRequest的CookieContainer为刚才建立的那个myCookieContainer
                Stream myRequestStream = myHttpWebRequest.GetRequestStream();
                StreamWriter myStreamWriter = new StreamWriter(myRequestStream, Encoding.GetEncoding("gb2312"));
                myStreamWriter.Write(indata);
                //把数据写入HttpWebRequest的Request流
                myStreamWriter.Close();
                myRequestStream.Close();

                //关闭打开对象
                HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
                //新建一个HttpWebResponse
                myHttpWebResponse.Cookies = myCookieContainer.GetCookies(myHttpWebRequest.RequestUri);
                //获取一个包含url的Cookie集合的CookieCollection
                Stream myResponseStream = myHttpWebResponse.GetResponseStream();
                StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("gb2312"));
                outdata = myStreamReader.ReadToEnd();
                webBrowser1.DocumentText = outdata;
                //把数据从HttpWebResponse的Response流中读出
                myStreamReader.Close();
                myResponseStream.Close();

    2010年3月9日 4:00

答案

全部回复