自动登陆新浪的博客,并且获取里面的源码

  private void startEnter()
    {
        //调用以上的方法来获取需要登录才能查看的内容。    
        CookieContainer cc = new CookieContainer();
        cc = Get_SinaLogin();        //获取登录Cookies

        string PhotoClassURL = "http://blog.sina.com.cn/myblog/htmlsource/blog_notopen.php?uid=1639008895";
        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();
        //sHtml即为你登录之后看到的内容.
       // MessageBox.Show(sHtml);
        this.TextBox2.Text = sHtml;

        for (int i = 0; i < Request.QueryString.AllKeys.Length; i++)
        {
            Response.Write(Request.QueryString.AllKeys[i] + "<br>");
        }  

    }

    public static CookieContainer Get_SinaLogin()
    {
        CookieContainer cc = new CookieContainer();
        string FormURL = "http://my.blog.sina.com.cn/login.php?index=index&type=new";                //处理表单的绝对URL地址
        string FormData = "loginname=youname&passwd=youpassword";    //表单需要提交的参数,注意改为你已注册的信息。

        Encoding encoding = Encoding.UTF8;
        byte[] data = encoding.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 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322)";
        //模拟一个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;
    }


   本人博客的文章大部分来自网络转载,因为时间的关系,没有写明转载出处和作者。所以在些郑重的说明:文章只限交流,版权归作者。谢谢

原文地址:https://www.cnblogs.com/wzg0319/p/1546856.html