QQ校友农场助手(有源码)

QQ农场助手,仿QQ伴侣界面,简单的功能已经可以用,VS 2005开发的。分析http请求工具,可以用HttpAnalyzer。

 关键代码:

登录代码
#region QQ登陆时必备函数
publicstaticstring binl2hex(byte[] buffer)
{
StringBuilder builder
=new StringBuilder();
for (int i =0; i < buffer.Length; i++)
{
builder.Append(buffer[i].ToString(
"x2"));
}
return builder.ToString();
}
publicstaticstring md5_3(string input)
{
MD5 md
= MD5.Create();
byte[] buffer = md.ComputeHash(Encoding.Default.GetBytes(input));
buffer
= md.ComputeHash(buffer);
buffer
= md.ComputeHash(buffer);
return binl2hex(buffer);
}

publicstaticstring md5(string input)
{
byte[] buffer = MD5.Create().ComputeHash(Encoding.Default.GetBytes(input));
return binl2hex(buffer);
}
#endregion

///<summary>
/// 获取加密后的密码
///</summary>
///<param name="password"></param>
///<param name="verifycode"></param>
///<returns></returns>
publicstaticstring getPassword(string password, string verifycode)
{
return md5(md5_3(password).ToUpper() + verifycode.ToUpper()).ToUpper();
}

///<summary>
/// //QQ农场的farmTime
///</summary>
///<returns></returns>
publicstaticint GetfarmTime()
{
TimeSpan t
= DateTime.Now.Subtract(DateTime.Parse("1970-1-1 08:00:00"));
return (int)t.TotalSeconds;
}
/// 获得FarmKey
publicstaticstring GetFarmKey(string farmTime)
{
return md5(farmTime +"sdoit78sdopig7w34057".Substring(Convert.ToInt32(farmTime) %10));
}

  

Unix时间戳
///<summary>
/// 得到某个时间增加某个小时后的Unix时间戳
///</summary>
///<param name="dt"></param>
///<param name="hours"></param>
///<returns></returns>
publicstaticstring GetUnixtimeStamp(DateTime dt, int hours)
{
double temp =double.Parse(hours.ToString());
dt
= dt.AddHours(temp);
return GetUnixTimeStamp(dt);
}
///<summary>
/// 得到某个时间的Unix时间戳
///</summary>
///<param name="dt"></param>
///<returns></returns>
publicstaticstring GetUnixTimeStamp(DateTime dt)
{
DateTime unixStartTime
= TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
TimeSpan timeSpan
= dt.Subtract(unixStartTime);
string timeStamp = timeSpan.Ticks.ToString();

return timeStamp.Substring(0, timeStamp.Length -7);
}
///<summary>
/// 得到QQ验证码
///</summary>
///<returns></returns>
public Stream GetValidateCode()
{
int random =new Random().Next(10000, 1000000);
Stream s
= httpHelper.GetStream("http://ptlogin2.qq.com/getimage?aid=15000102&"+ random, CookieMng.CurrentCookie);
return s;
}

 

Json数据处理
///<summary>
/// 处理json数据,返回Dictionary对象
///</summary>
///<param name="jsondata"></param>
///<returns></returns>
publicstatic Dictionary<string, object> Deserialize(string jsondata)
{
JavaScriptSerializer serializer
=new JavaScriptSerializer();
Dictionary
<string, object> json = (Dictionary<string, object>)serializer.DeserializeObject(jsondata);
return json;
}
///<summary>
/// 处理json数据,返回object对象
///</summary>
///<param name="jsondata"></param>
///<returns></returns>
publicstaticobject[] DeserializeToObject(string jsondata)
{
JavaScriptSerializer serializer
=new JavaScriptSerializer();
object[] json = (object[])serializer.DeserializeObject(jsondata);
return json;
}

 

Cookie的管理
public staticclass CookieMng
{
privatestatic CookieContainer ccontainer;

///<summary>
/// 静态构造函数
///</summary>
static CookieMng()
{
ccontainer
=new CookieContainer();
}
///<summary>
/// 获取当前的cookie
///</summary>
publicstatic CookieContainer CurrentCookie
{
get { return ccontainer; }
set { ccontainer = value; }
}
}

 

HttpHelper类
publicclass HttpHelper
{
#region 私有变量
private CookieContainer cc;
privatestring contentType ="application/x-www-form-urlencoded";
privatestring accept ="image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/x-silverlight, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/x-silverlight-2-b1, */*";
privatestring userAgent ="Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
private Encoding encoding = Encoding.GetEncoding("utf-8");
privateint delay =3000;
privateint maxTry =3;
privateint currentTry =0;
#endregion


publicstatic FrmValidateCode frmValidateCode;
#region 属性
///<summary>
/// Cookie容器
///</summary>
public CookieContainer CookieContainer
{
get
{
return cc;
}
}

///<summary>
/// 获取网页源码时使用的编码
///</summary>
///<value></value>
public Encoding Encoding
{
get
{
return encoding;
}
set
{
encoding
= value;
}
}

publicint NetworkDelay
{
get
{
Random r
=new Random();
return (r.Next(delay /1000, delay /1000*2)) *1000;
}
set
{
delay
= value;
}
}

publicint MaxTry
{
get
{
return maxTry;
}
set
{
maxTry
= value;
}
}
#endregion

#region 构造函数
///<summary>
/// Initializes a new instance of the <see cref="HttpHelper"/> class.
///</summary>
public HttpHelper()
{
cc
=new CookieContainer();
}

///<summary>
/// Initializes a new instance of the <see cref="HttpHelper"/> class.
///</summary>
///<param name="cc">The cc.</param>
public HttpHelper(CookieContainer cc)
{
this.cc = cc;
}

///<summary>
/// Initializes a new instance of the <see cref="HttpHelper"/> class.
///</summary>
///<param name="contentType">Type of the content.</param>
///<param name="accept">The accept.</param>
///<param name="userAgent">The user agent.</param>
public HttpHelper(string contentType, string accept, string userAgent)
{
this.contentType = contentType;
this.accept = accept;
this.userAgent = userAgent;
}

///<summary>
/// Initializes a new instance of the <see cref="HttpHelper"/> class.
///</summary>
///<param name="cc">The cc.</param>
///<param name="contentType">Type of the content.</param>
///<param name="accept">The accept.</param>
///<param name="userAgent">The user agent.</param>
public HttpHelper(CookieContainer cc, string contentType, string accept, string userAgent)
{
this.cc = cc;
this.contentType = contentType;
this.accept = accept;
this.userAgent = userAgent;
}
#endregion

#region 公共方法
///<summary>
/// 获取指定页面的HTML代码
///</summary>
///<param name="url">指定页面的路径</param>
///<param name="postData">回发的数据</param>
///<param name="isPost">是否以post方式发送请求</param>
///<param name="cookieCollection">Cookie集合</param>
///<returns></returns>
publicstring GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)
{
if (string.IsNullOrEmpty(postData))
{
return GetHtml(url, cookieContainer);
}
currentTry
++;
try
{
byte[] byteRequest = Encoding.Default.GetBytes(postData);

HttpWebRequest httpWebRequest;
httpWebRequest
= (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer
= cookieContainer;
httpWebRequest.ContentType
= contentType;
httpWebRequest.Referer
= url;
httpWebRequest.Accept
= accept;
httpWebRequest.UserAgent
= userAgent;
httpWebRequest.Method
= isPost ?"POST" : "GET";
httpWebRequest.ContentLength
= byteRequest.Length;

Stream stream
= httpWebRequest.GetRequestStream();
stream.Write(byteRequest,
0, byteRequest.Length);
stream.Close();

HttpWebResponse httpWebResponse;
httpWebResponse
= (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream
= httpWebResponse.GetResponseStream();
StreamReader streamReader
=new StreamReader(responseStream, encoding);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();

currentTry
=0;

if (html.Contains("validateCode"))
{
if (string.IsNullOrEmpty(QQ.FriendQQ))
{
QQ.FriendQQ
= QQ.CurrentQQ;
}
if (frmValidateCode ==null)
{
frmValidateCode
=new FrmValidateCode();//将主窗体对象传递过去
frmValidateCode.ShowDialog();
}
else
{
frmValidateCode.Activate();
if (!frmValidateCode.Visible)
{
frmValidateCode.ShowDialog();
}
}
returnnull;
}
new Log().WriteLog(html); //记录每一次请求的html代码
return html;
}
catch (Exception e)
{
//Console.ForegroundColor = ConsoleColor.Red;
//Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
//Console.ForegroundColor = ConsoleColor.White;

//if (currentTry <= maxTry)
//{
// GetHtml(url, postData, isPost, cookieContainer);
//}

//currentTry = 0;
returnstring.Empty;
}
}

///<summary>
/// 获取指定页面的HTML代码
///</summary>
///<param name="url">指定页面的路径</param>
///<param name="cookieCollection">Cookie集合</param>
///<returns></returns>
publicstring GetHtml(string url, CookieContainer cookieContainer)
{
Thread.Sleep(NetworkDelay);

currentTry
++;

try
{
HttpWebRequest httpWebRequest;
httpWebRequest
= (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer
= cookieContainer;
httpWebRequest.ContentType
= contentType;
httpWebRequest.Referer
= url;
httpWebRequest.Accept
= accept;
httpWebRequest.UserAgent
= userAgent;
httpWebRequest.Method
="GET";

HttpWebResponse httpWebResponse;
httpWebResponse
= (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream
= httpWebResponse.GetResponseStream();
StreamReader streamReader
=new StreamReader(responseStream, encoding);
string html = streamReader.ReadToEnd();
streamReader.Close();
responseStream.Close();

currentTry
=0;
if (html.Contains("validateCode"))
{
if (string.IsNullOrEmpty(QQ.FriendQQ))
{
QQ.FriendQQ
= QQ.CurrentQQ;
}
if (frmValidateCode ==null)
{
frmValidateCode
=new FrmValidateCode();//将主窗体对象传递过去
frmValidateCode.ShowDialog();
}
else
{
frmValidateCode.Activate();
if (!frmValidateCode.Visible)
{
frmValidateCode.ShowDialog();
}
}
returnnull;
}
return html;
}
catch (Exception e)
{
//Console.ForegroundColor = ConsoleColor.Red;
//Console.WriteLine(DateTime.Now.ToString("HH:mm:ss ") + e.Message);
//Console.ForegroundColor = ConsoleColor.White;

//if (currentTry <= maxTry)
//{
// GetHtml(url, cookieContainer);
//}

//currentTry = 0;
returnstring.Empty;
}
}

///<summary>
/// 获取指定页面的HTML代码
///</summary>
///<param name="url">指定页面的路径</param>
///<returns></returns>
publicstring GetHtml(string url)
{
return GetHtml(url, cc);
}

///<summary>
/// 获取指定页面的HTML代码
///</summary>
///<param name="url">指定页面的路径</param>
///<param name="postData">回发的数据</param>
///<param name="isPost">是否以post方式发送请求</param>
///<returns></returns>
publicstring GetHtml(string url, string postData, bool isPost)
{
return GetHtml(url, postData, isPost, cc);
}

///<summary>
/// 获取指定页面的Stream
///</summary>
///<param name="url">指定页面的路径</param>
///<param name="postData">回发的数据</param>
///<param name="isPost">是否以post方式发送请求</param>
///<param name="cookieCollection">Cookie集合</param>
///<returns></returns>
public Stream GetStream(string url, CookieContainer cookieContainer)
{
currentTry
++;
try
{
HttpWebRequest httpWebRequest;
httpWebRequest
= (HttpWebRequest)HttpWebRequest.Create(url);
httpWebRequest.CookieContainer
= cookieContainer;
httpWebRequest.ContentType
= contentType;
httpWebRequest.Referer
= url;
httpWebRequest.Accept
= accept;
httpWebRequest.UserAgent
= userAgent;
httpWebRequest.Method
="GET";

HttpWebResponse httpWebResponse;
httpWebResponse
= (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream
= httpWebResponse.GetResponseStream();
currentTry
=0;

CookieCollection cc
= cookieContainer.GetCookies(new Uri(url));
return responseStream;
}
catch (Exception e)
{
Console.ForegroundColor
= ConsoleColor.Red;
Console.WriteLine(DateTime.Now.ToString(
"HH:mm:ss ") + e.Message);
Console.ForegroundColor
= ConsoleColor.White;

if (currentTry <= maxTry)
{
GetHtml(url, cookieContainer);
}

currentTry
=0;
returnnull;
}
}
#endregion
}

  

下载  源代码下载

posted on 2010-01-10 17:27  几度夕阳红了  阅读(1467)  评论(10编辑  收藏  举报