阿里妈妈自动登录程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net;
using System.IO;
using System.Text;
using System.Xml;
using System.IO.Compression;

namespace MvcApplication1.Controllers
{
    public class IndexController : Controller
    {
        //
        // GET: /Default1/

        public ContentResult Index()
        {
            
            return Content(getXMLDocumentFromXMLTemplate("http://www.alimama.com/index.htm"));
        }

        public string getXMLDocumentFromXMLTemplate(string inURL)
        {
            HttpWebRequest myHttpWebRequest = null;     
            HttpWebResponse myHttpWebResponse = null;

            //第一次请求信息的获取
            myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(inURL);
            myHttpWebRequest.Method = "GET";
            //设置请求头信息
            SetGetHeaders(myHttpWebRequest);
            //Get Response
            myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

            //取出第一次cookies信息,建立引用
            CookieCollection first = myHttpWebResponse.Cookies;


            //第二次请求开始
            myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://www.alimama.com/member/minilogin_act.htm");
            myHttpWebRequest.Method = "POST";
            myHttpWebRequest.ContentType = "application/x-www-form-urlencoded";
            //设置请求头信息
            SetGetHeaders(myHttpWebRequest);
            myHttpWebRequest.CookieContainer.Add(first);
            myHttpWebRequest.Referer = "http://www.alimama.com/membersvc/member/login.htm";
            //302 不做跳转
            myHttpWebRequest.AllowAutoRedirect = false;

            string Post = "_tb_token_=" + first["_tb_token_"].Value + "&style=&redirect=&proxy=http%3A%2F%2Fwww.alimama.com%2Fproxy.htm&logname=<*****>%40qq.com&originalLogpasswd=<*****>&logpasswd=c781f1ee5ab17dff12ff6019325688f2";

            byte[] bs = Encoding.ASCII.GetBytes(Post);

            myHttpWebRequest.ContentLength = bs.Length;
           using (Stream reqStream = myHttpWebRequest.GetRequestStream())
           {
               reqStream.Write(bs, 0, bs.Length);
               reqStream.Close();
           }
            myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

            //取出第二次cookies信息,建立引用(无临时重定向)
            CookieCollection second = myHttpWebResponse.Cookies;

            //模拟浏览器重定向 

            myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create("https://www.alimama.com/member/minilogin_success.htm");
            myHttpWebRequest.Method = "GET";
            //设置请求头信息
            SetGetHeaders(myHttpWebRequest);
            myHttpWebRequest.CookieContainer.Add(second);
            //Get Response
            myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

            second["rurl"].Value = "";

            second["v"].Value = "0";

            //请求列表页面,第四次请求
            myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create("http://ad.alimama.com/account/incomeDetail.htm");
            myHttpWebRequest.Method = "GET";
            //设置请求头信息
            SetGetHeaders(myHttpWebRequest);

            myHttpWebRequest.CookieContainer.Add(second);
            myHttpWebRequest.CookieContainer.Add(first["t"]);
            myHttpWebRequest.CookieContainer.Add(first["cookie2"]);
            //Get Response
            myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

            StreamReader readers = new StreamReader(myHttpWebResponse.GetResponseStream(), Encoding.UTF8);
            string text = readers.ReadToEnd();

            readers.Close();
            readers.Dispose();
            return text;
        }
        public void SetGetHeaders(HttpWebRequest request)
        {
            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            //request.Headers.Add("Accept-Encoding", "gzip, deflate");
            request.Headers.Add("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3");
            request.Host = "www.alimama.com";
            request.KeepAlive = true;
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0";
            request.CookieContainer = new CookieContainer();
        }
    }
}

 

你可能感兴趣的:(登录)