C#自动登录人人
最近在学习C#的通信一章,写了一个自动登录人人的demo,纯属娱乐哈~
首先用firebug分析一下人人登陆到底发送了些什么东西,主要的内容有以下3个,一个POST和两个GET,其他参数(涉及密码),大家可以自己用firebug看一下,非常方便
所以想了一下的解决办法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.Web;
namespace ConsoleApplication1
{
class Program
{
static string HostUrl = "http%3A%2F%2Fwww.renren.com%2FHome.do";
static string hosturl = "http://.renren.com%/Home.do";
static string LoginUrl = "http://passport.renren.com/PLogin.do";
static CookieContainer cookecn = new CookieContainer();
static string email = "youremail%40sina.com"; //@符号用40%代替例如:example40%gmail.com
static string pwd = "yourpassword"; //你的密码
static string firstgethost = string.Empty;
static string Cookiesstr = string.Empty;
static void Main(string[] args)
{
Console.Write("the first is post:\n");
string postdata = "email=" + email + "&password=" + pwd + "&origURL=" + HostUrl + "&domain=renren.com";
PostData(postdata);
Console.Write("next host is:\n");
Console.Write(firstgethost);
FirstGet(firstgethost, Cookiesstr);
SecondGet(HostUrl);
Console.ReadKey();
}
static bool PostData(string postdata)
{
HttpWebRequest request = null;
HttpWebResponse response = null;
try
{
request = (HttpWebRequest)WebRequest.Create(LoginUrl);
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
byte[] postdatabytes = Encoding.Default.GetBytes(postdata);
char[] chars = new char[500];
for (int i = 0; i < postdatabytes.Length; i++) {
chars[i] = (char)postdatabytes[i];
}
request.ContentLength = postdatabytes.Length;
request.Referer = "http://www.renren.com/";
request.AllowAutoRedirect = false;
request.CookieContainer = new CookieContainer();
request.KeepAlive = true;
Stream requeststream = request.GetRequestStream();
//response = (HttpWebResponse)request.GetResponse();
//Console.WriteLine("\nThe value of 'HaveResponse' property before a response object is obtained :{0}", request.HaveResponse);
requeststream.Write(postdatabytes, 0, postdatabytes.Length);
requeststream.Close();
response = (HttpWebResponse)request.GetResponse();
response.Cookies = request.CookieContainer.GetCookies(request.RequestUri);
CookieCollection cook = response.Cookies;
string strcrook = request.CookieContainer.GetCookieHeader(request.RequestUri);
Cookiesstr = strcrook;
Console.Write(strcrook);
Console.Write("\n");
for (int i = 0; i < cook.Count; i++)
{
Console.Write(cook[i]);
Console.Write("\n");
}
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string content = sr.ReadToEnd();
response.Close();
string[] substr = content.Split(new char[] { '"' });
firstgethost = substr[1];
Console.Write("content is :\n");
Console.Write(content);
}
catch (System.Exception e)
{
Console.Write("error!");
return false;
}
return true;
}
static bool FirstGet(string url, string cookies)
{
Console.Write("\n*********************************************************");
HttpWebRequest request = null;
HttpWebResponse respone = null;
CookieContainer cnter = new CookieContainer();
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.KeepAlive = true;
request.ContentType = "text.html";
request.Headers.Add("Cookie:" + cookies);
request.CookieContainer = cnter;
request.AllowAutoRedirect = false;
request.Referer = "http://www.renren.com/SysHome.do";
respone = (HttpWebResponse)request.GetResponse();
string iCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);
Cookiesstr = iCookies;
StreamReader sr = new StreamReader(respone.GetResponseStream(), Encoding.UTF8);
string ss = sr.ReadToEnd();
Console.Write("\n");
Console.Write(ss);
Console.Write("\n");
Console.Write(iCookies);
request.Abort();
sr.Close();
respone.Close();
}
catch(System.Exception e)
{
Console.Write("\nit is erroring");
}
return true;
}
static bool SecondGet(string url)
{
Console.Write("\n************************************************************");
HttpWebRequest request = null;
HttpWebResponse respone = null;
CookieContainer cnter = new CookieContainer();
try
{
request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
request.KeepAlive = true;
request.ContentType = "text/html";
request.Headers.Add("Cookie:" + Cookiesstr);
request.CookieContainer = cnter;
request.AllowAutoRedirect = false;
request.Referer = "http://www.renren.com/SysHome.do";
respone = (HttpWebResponse)request.GetResponse();
string iCookies = request.CookieContainer.GetCookieHeader(request.RequestUri);
StreamReader sr = new StreamReader(respone.GetResponseStream(), Encoding.UTF8);
string ss = sr.ReadToEnd();
Console.Write("\n");
Console.Write(ss);
Console.Write("\n");
Console.Write(iCookies);
//request.Abort();
sr.Close();
respone.Close();
}
catch (System.Exception e)
{
Console.Write("\nit is erroring");
}
return true;
}
}
}