using System;
using System.Collections.Generic;
using System.Text;
using Chilkat;//使用的是第三方库Chilkat,需要到官网下载
using System.IO;
using HtmlProcessor;
namespace XiaoneiChilkat
{
class Program
{
static void Main(string[] args)
{
Chilkat.HttpRequest req = new Chilkat.HttpRequest();
Chilkat.Http http = new Chilkat.Http();
bool success;
// Any string unlocks the component for the 1st 30-days.
success = http.UnlockComponent("Anything for 30-day trial");
if (success != true)
{
Console.WriteLine(http.LastErrorText);
return;
}
http.CookieDir = @"C:/Documents and Settings/Administrator/Cookies";
http.SaveCookies = true;
http.SendCookies = true;
http.AcceptLanguage = "zh-cn";
http.Referer = "http://www.renren.com/SysHome.do";
http.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022)";
http.Connection = "Keep-Alive";
string html;
html = http.QuickGetStr("http://www.renren.com/GLogin.do");
if (http.LastStatus != 200)
{
Console.WriteLine(http.LastErrorText);
return;
}
string pwd = "";
Console.WriteLine("Pwd:");
pwd=Console.ReadLine();
req.UsePost();
req.Path = "/PLogin.do";
req.AddParam("email", "你的登陆邮箱");
req.AddParam("password", pwd);//登陆密码
req.AddParam("origURL", "");
req.AddParam("domain", "renren.com");
req.AddParam("formName", "");
req.AddParam("method", "");
req.AddParam("isplogin", "true");
req.AddParam("submit", "登录");
string domain="www.renren.com";
int port=80;
bool ssl=false;
Chilkat.HttpResponse resp = null;
http.FollowRedirects = false;
resp = http.SynchronousRequest(domain, port, ssl, req);
string text="";
if (resp == null)
{
text += http.LastErrorText + "/r/n";
}
else
{
int responseStatus;
responseStatus = resp.StatusCode;
if (responseStatus == 302)
{
http.FollowRedirects = true;
string redUrl = resp.GetHeaderField("Location");
html = http.QuickGetStr(redUrl);
if (http.LastStatus != 200)
{
Console.WriteLine("登录失败:" + http.LastErrorText);
return;
}
else
{
//WARNING:注意要改为你所登录的校内的账户的ID!
string mainPage = "http://www.renren.com/profile.do?id=你的校内Id";
html = http.QuickGetStr(mainPage);
if (http.LastStatus != 200)
{
Console.WriteLine("转到主页失败:" + http.LastErrorText);
return;
}
HtmlParser parser = new HtmlParser();
XiaoNeiFriend frid = new XiaoNeiFriend("self", mainPage);
string textOfFriend="";
bool visitedSucceed = true;
int totalCount = 0;
int successCount = 0;
string sex = "Boy";
System.DateTime time = DateTime.Now;
//创建日志文件,以当前日期时间为文件名
StringBuilder sb=new StringBuilder(@"d:/xiaonei/log/");
string str = time.ToString("s");
sb.Append(str.Replace(':', '_'));
sb.Append(".txt");
StreamWriter writer = new StreamWriter(sb.ToString());
//创建随机数,用于线程sleep
Random rnd = new Random(time.Millisecond);
//遍历次数需要控制;若为无限循环靠^c结束程序就不能写入文件
int visit =130;
int i = 0;
for (; i < visit;i++ )
{
html = http.QuickGetStr(frid.Href);
if (http.LastStatus != 200)
{
Console.WriteLine("转到{0}失败:{1} " + http.LastErrorText, frid.FriendName, http.LastStatus);
//writer.Close();
break;
}
if (0==i)
{
textOfFriend = parser.AbstractFriends(html);
if (string.IsNullOrEmpty(textOfFriend))
{
Console.WriteLine("error:abstract friends return null! Id={0}", frid.Href);
// writer.Close();
break;
}
}
http.Referer = frid.Href;
totalCount++;
if (visitedSucceed)
{
successCount++;
}
//日志输出
time = DateTime.Now;
writer.WriteLine("{0}({1}):{2}/t{3,10} ",totalCount, successCount, time.ToLongTimeString(), frid.FriendName);
Console.WriteLine("{0}({1}):{2}/t{3,10}", totalCount, successCount, time.ToLongTimeString(), frid.FriendName);
if (0==i)
{
bool b = parser.GetAllFriends(textOfFriend);
if (b == false)
{
Console.WriteLine("error:null friend reference!");
//writer.Close();
break;
}
}
if (i >= parser.friendList.Count)
{
break;
}
frid = parser.friendList[i];
//sleep:时间不能过大,否则会报连接关闭错误
int delay = rnd.Next(2500, 9000);
Console.WriteLine("delay:{0}", delay);
System.Threading.Thread.Sleep(delay);
}
if (i!=visit)
{
writer.WriteLine("意外终止!");
}
else
{
writer.WriteLine("正常终止!");
}
writer.WriteLine("访问成功比例:{0}", successCount / (double)totalCount);
writer.Close();
Console.WriteLine("访问成功比例:{0}", successCount / (double)totalCount);
}
// text += html + "/r/n";
}
else
{
Console.WriteLine("Response Status={0},No redict....",responseStatus);
// Display the HTML page returned.
text += resp.BodyStr + "/r/n";
//Console.WriteLine(text);
}
}
}
}
}
....