模拟登录--OPPO官网

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace renrenApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://account.oppo.com/sysadmin/htm/index.php?q=user/login&back=http%3A%2F%2Fbbs.myoppo.com%2F";
            Encoding m_Encoding = Encoding.GetEncoding("utf-8");
            GetHtmlPage(url, m_Encoding);
        }

        #region GET方法获得页面的HTML代码
        private static string GetHtmlPage(string URL, Encoding encodingFormat)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            CookieContainer cc = new CookieContainer();
            StreamReader sr = null;
            string strCookies = string.Empty;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(URL);//实例化web访问类
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
                request.ContentType = "application/x-www-form-urlencoded";//模拟头
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                request.Method = "GET";
                request.KeepAlive = true;
                request.Headers.Add("Cookie:" + strCookies);
                response = (HttpWebResponse)request.GetResponse();
                //设置cookie   
                strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);
                //取再次跳转链接   
                sr = new StreamReader(response.GetResponseStream(), encodingFormat);
                string content = sr.ReadToEnd();
                //return content;
            }
            catch (Exception ex)//GET出错
            {
                Console.WriteLine("远程服务器返回错误");
            }
            try
            {
                //第一次POST请求
                string postdata = string.Format("ctime={0}&login_user_acc=xxx&login_user_pass=xxx", DateTime.Now.ToString("yyyy-MM-dd hh:mm"));//模拟请求数据,数据样式可以用FireBug插件得到。人人网POST数据时,用户名邮箱中的“@”变为“%40”,所以我们也要作此变化
                string LoginUrl = "http://account.oppo.com/sysadmin/htm/index.php?q=user/ajaxlogin";
                request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类
                request.Method = "POST";//数据提交方式为POST
                //模拟头
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
                request.ContentLength = postdatabytes.Length;
                request.Referer = "http://account.oppo.com/sysadmin/htm/index.php?q=user/ajaxlogin";
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
                // request.ContentType = "text/html";
                request.ContentType = "application/x-www-form-urlencoded";//模拟头
                //提交请求
                Stream stream;
                stream = request.GetRequestStream();
                stream.Write(postdatabytes, 0, postdatabytes.Length);
                stream.Close();
                //接收响应
                response = (HttpWebResponse)request.GetResponse();
                request.Headers.Add("Cookie:" + strCookies);
                request.CookieContainer = cc;
                //保存返回cookie
                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
                CookieCollection cook = response.Cookies;
                string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
                sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string content = sr.ReadToEnd();
            }
            catch (Exception)
            {
                //第一次POST出错;
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                }
                if (response != null)
                {
                    response.Close();
                }
                if (sr != null)
                {
                    sr.Close();
                }
            }
            return "";
        }
        #endregion

    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

namespace renrenApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string url = "http://account.oppo.com/sysadmin/htm/index.php?q=user/login&back=http%3A%2F%2Fbbs.myoppo.com%2F";
            Encoding m_Encoding = Encoding.GetEncoding("utf-8");
            GetHtmlPage(url, m_Encoding);
        }

        #region GET方法获得页面的HTML代码
        private static string GetHtmlPage(string URL, Encoding encodingFormat)
        {
            HttpWebRequest request = null;
            HttpWebResponse response = null;
            CookieContainer cc = new CookieContainer();
            StreamReader sr = null;
            string strCookies = string.Empty;
            try
            {
                request = (HttpWebRequest)WebRequest.Create(URL);//实例化web访问类
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
                request.ContentType = "application/x-www-form-urlencoded";//模拟头
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                request.Method = "GET";
                request.KeepAlive = true;
                request.Headers.Add("Cookie:" + strCookies);
                response = (HttpWebResponse)request.GetResponse();
                //设置cookie   
                strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);
                //取再次跳转链接   
                sr = new StreamReader(response.GetResponseStream(), encodingFormat);
                string content = sr.ReadToEnd();
                //return content;
            }
            catch (Exception ex)//GET出错
            {
                Console.WriteLine("远程服务器返回错误");
            }
            try
            {
                //第一次POST请求
                string postdata = string.Format("ctime={0}&login_user_acc=xxx&login_user_pass=xxx", DateTime.Now.ToString("yyyy-MM-dd hh:mm"));//模拟请求数据,数据样式可以用FireBug插件得到。人人网POST数据时,用户名邮箱中的“@”变为“%40”,所以我们也要作此变化
                string LoginUrl = "http://account.oppo.com/sysadmin/htm/index.php?q=user/ajaxlogin";
                request = (HttpWebRequest)WebRequest.Create(LoginUrl);//实例化web访问类
                request.Method = "POST";//数据提交方式为POST
                //模拟头
                request.ContentType = "application/x-www-form-urlencoded";
                byte[] postdatabytes = Encoding.UTF8.GetBytes(postdata);
                request.ContentLength = postdatabytes.Length;
                request.Referer = "http://account.oppo.com/sysadmin/htm/index.php?q=user/ajaxlogin";
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
                // request.ContentType = "text/html";
                request.ContentType = "application/x-www-form-urlencoded";//模拟头
                //提交请求
                Stream stream;
                stream = request.GetRequestStream();
                stream.Write(postdatabytes, 0, postdatabytes.Length);
                stream.Close();
                //接收响应
                response = (HttpWebResponse)request.GetResponse();
                request.Headers.Add("Cookie:" + strCookies);
                request.CookieContainer = cc;
                //保存返回cookie
                response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
                CookieCollection cook = response.Cookies;
                strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);
                sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                string content = sr.ReadToEnd();
            }
            catch (Exception)
            {
                //第一次POST出错;
            }
            try
            {
                URL = "http://account.oppo.com/sysadmin/htm/index.php?q=index/crossdomain&domain=bbs.myoppo.com&loginfile=setssocookie.php";
                request = (HttpWebRequest)WebRequest.Create(URL);//实例化web访问类
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
                request.ContentType = "application/x-www-form-urlencoded";//模拟头
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                request.Method = "GET";
                request.KeepAlive = true;
                request.Headers.Add("Cookie:" + strCookies);
                response = (HttpWebResponse)request.GetResponse();
                //设置cookie   
                strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);
                //取再次跳转链接   
                sr = new StreamReader(response.GetResponseStream(), encodingFormat);
                string content = sr.ReadToEnd();
                URL = response.Headers["location"];
                //return content;
            }
            catch (Exception ex)//GET出错
            {
                Console.WriteLine("远程服务器返回错误");
            }
            try
            {            
                request = (HttpWebRequest)WebRequest.Create(URL);//实例化web访问类
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
                request.ContentType = "application/x-www-form-urlencoded";//模拟头
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                request.Method = "GET";
                request.KeepAlive = true;
                request.Headers.Add("Cookie:" + strCookies);
                response = (HttpWebResponse)request.GetResponse();
                //设置cookie   
                strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);
                //取再次跳转链接   
                sr = new StreamReader(response.GetResponseStream(), encodingFormat);
                string content = sr.ReadToEnd();
            }
            catch (Exception ex)//GET出错
            {
                Console.WriteLine("远程服务器返回错误");
            }
            try
            {
                URL = "http://bbs.myoppo.com/search.php?mod=forum&ascdesc=desc&before=&formhash=0fd16888&orderby=dateline&searchsubmit=yes&searchsubmit=yes&srchfid%5B%5D=all&srchfilter=all&srchfrom=0&srchtxt=oppo&srchuname=";
                request = (HttpWebRequest)WebRequest.Create(URL);//实例化web访问类
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
                request.ContentType = "application/x-www-form-urlencoded";//模拟头
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                request.Method = "GET";
                request.KeepAlive = true;
                request.Headers.Add("Cookie:" + strCookies);
                response = (HttpWebResponse)request.GetResponse();
                //设置cookie   
                strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);
                //取再次跳转链接   
                sr = new StreamReader(response.GetResponseStream(), encodingFormat);
                string content = sr.ReadToEnd();
                URL = "http://bbs.myoppo.com/" + response.Headers["location"];
                //return content;
            }
            catch (Exception ex)//GET出错
            {
                Console.WriteLine("远程服务器返回错误");
            }
            try
            {
               
                request = (HttpWebRequest)WebRequest.Create(URL);//实例化web访问类
                request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
                request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
                request.ContentType = "application/x-www-form-urlencoded";//模拟头
                request.AllowAutoRedirect = false;
                request.CookieContainer = cc;
                request.KeepAlive = true;
                request.Method = "GET";
                request.KeepAlive = true;
                request.Headers.Add("Cookie:" + strCookies);
                response = (HttpWebResponse)request.GetResponse();
                //设置cookie   
                strCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);
                //取再次跳转链接   
                sr = new StreamReader(response.GetResponseStream(), encodingFormat);
                string content = sr.ReadToEnd();
                return content;
            }
            catch (Exception ex)//GET出错
            {
                Console.WriteLine("远程服务器返回错误");
            }
            finally
            {
                if (request != null)
                {
                    request.Abort();
                }
                if (response != null)
                {
                    response.Close();
                }
                if (sr != null)
                {
                    sr.Close();
                }
            }
            return "";
        }
        #endregion

    }
}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值