谁哪里有例子呢?比如人人网的....天涯论坛的等等,希望有的能给我发一份,一定要能用呀,从网上找了几个,都不行!
发了的,记着在下面留言,直接给分!200

解决方案 »

  1.   

      CookieContainer myCookieContainer = new CookieContainer();//cookie,全局的         //登陆url,返回登录成功后网页代码,paramList为登录的提交字符串
            public string Login(String url, String paramList)
            {
                HttpWebResponse res = null;
                string strResult = "";
                string postdata = null;            try
                {
                    
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                    req.Method = "POST";
                    req.ContentType = "application/x-www-form-urlencoded";
                    //req.AllowAutoRedirect = false;
                    req.AllowWriteStreamBuffering = false;//禁用数据缓存
                    //if (myCookieContainer!=null)
                    //{
                    req.CookieContainer = myCookieContainer;
                    //}
                    //req.Connection = "keep - alive";
                    postdata = bianma(paramList);                byte[] data = Encoding.GetEncoding(charset).GetBytes(postdata);
                    req.ContentLength = data.Length;
                    Stream strm = req.GetRequestStream();
                    strm.Write(data, 0, data.Length);    //写入参数
                    strm.Close();
                    res = (HttpWebResponse)req.GetResponse();
                    res.Cookies = myCookieContainer.GetCookies(req.RequestUri);
                    //myCookieContainer.Add(res.Cookies);                
                    Stream ReceiveStream = res.GetResponseStream();
                    Encoding encode = System.Text.Encoding.GetEncoding(charset);
                    StreamReader sr = new StreamReader(ReceiveStream, encode);                strResult = sr.ReadToEnd();
                }
                catch (Exception e)
                {
                    strResult = e.ToString();
                }
                finally
                {
                    if (res != null)
                    {
                        res.Close();
                    }
                }
                return strResult;
            }
      /// <summary>
            //对参数字符串进行URL编码
            /// </summary>
            public string bianma(string postData)//对参数字符串进行URL编码
            {
                Encoding e1 = Encoding.GetEncoding(charset);
                int i = 0, j;
                StringBuilder UrlEncoded = new StringBuilder();
                Char[] reserved = { '?', '=', '&' };
                while (i < postData.Length)
                {
                    j = postData.IndexOfAny(reserved, i);//报告指定 Unicode 字符数组中的任意字符在此实例中第一个匹配项的索引。该搜索从指定字符位置开始。 
                    //j = 0 j=11
                    if (j == -1)
                    {
                        UrlEncoded.Append(HttpUtility.UrlEncode(postData.Substring(i, postData.Length - i), e1));
                        break;
                    }
                    UrlEncoded.Append(HttpUtility.UrlEncode(postData.Substring(i, j - i), e1));
                    //在此实例的结尾追加指定对象的字符串表示形式
                    //HttpUtility提供用于在处理 Web 请求时编码和解码 URL 的方法。
                    //UrlEncode()方法对 URL 字符串进行编码。
                    //Append函数在此实例的结尾追加指定数组的Unicode字符的字符串表示形式
                    UrlEncoded.Append(postData.Substring(j, 1));
                    i = j + 1;
                }
                // Encoding.Unicode.GetString(UrlEncoded);
                return UrlEncoded.ToString();        }
      

  2.   

    之后的页面获取操作比如论坛的发帖等使用myCookieContainer 即可
      

  3.   

    我有一个自动登录csdn的,采集自己的分数情况,一个小例子。不知你要吗?
      

  4.   

    好的,可以的,谢谢了。[email protected]