[C#] 纯文本查看 复制代码
HttpHelper http = new HttpHelper();
HttpItem item = new HttpItem()
{
URL = "http://www.baidu.com/",//URL 必需项
Encoding = System.Text.Encoding.GetEncoding("utf-8"),//URL 可选项 默认为Get
Method = "get",//URL 可选项 默认为Get
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = "",//字符串Cookie 可选项
Referer = "",//来源URL 可选项
Postdata = "",//Post数据 可选项GET时不需要写
Timeout = 100000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统 可选项有默认值
ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值
Allowautoredirect = false,//是否根据301跳转 可选项
ResultType = ResultType.String
};
HttpResult result = http.GetHtml(item);
string html = result.Html;
string cookie = result.Cookie;
item = new HttpItem()
{
URL = "http://passport.baidu.com/v2/api/?getapi&tpl=mn&apiver=v3&tt=1402545719625&class=login&logintype=dialogLogin&callback=bd__cbs__secnga",
Encoding = System.Text.Encoding.GetEncoding("utf-8"),//URL 可选项 默认为Get
Method = "get",//URL 可选项 默认为Get
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = cookie,//字符串Cookie 可选项
Referer = "",//来源URL 可选项
Postdata = "",//Post数据 可选项GET时不需要写
Timeout = 100000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统 可选项有默认值
ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值
Allowautoredirect = false,//是否根据301跳转 可选项
ResultType = ResultType.String
};
result = http.GetHtml(item);
html = result.Html;
string tooken = RegexHelper.regex_string_one("(?<=\"token\" : \").*?(?=\")", html);
cookie = result.Cookie;
cookie = cookie + "; " + item.Cookie;
string cookiestring = string.Empty;
foreach (CookieItem s in HttpCookieHelper.GetCookieList(cookie))
{
if (s.Key.Contains("BAIDUID") || s.Key.Contains("BDSVRTM") || s.Key.Contains("H_PS_PSSID") || s.Key.Contains("HOSUPPORT"))
{
cookiestring = cookiestring + HttpCookieHelper.CookieFormat(s.Key, s.Value);
}
}
item = new HttpItem()
{
URL = "http://passport.baidu.com/v2/api/?login",//URL 必需项
Encoding = System.Text.Encoding.GetEncoding("utf-8"),//URL 可选项 默认为Get
Method = "post",//URL 可选项 默认为Get
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = cookiestring,//字符串Cookie 可选项
Referer = "http://www.baidu.com/",//来源URL 可选项
Postdata = "staticpage=http%3A%2F%2Fhi.baidu.com%2Fcom%2Fshow%2Fproxy%3Ffor%3Dv3_pass_login&charset=utf-8&token=" + tooken + "&tpl=qing&apiver=v3&tt=1372661616626&codestring=&isPhone=false&safeflg=0&u=http%3A%2F%2Fhi.baidu.com%2F&username=shijiaxin3180&password=test123456&verifycode=&mem_pass=on&ppui_logintime=165501&callback=parent.bd__pcbs__hggm3p",//Post数据 可选项GET时不需要写
Timeout = 100000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统 可选项有默认值
ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值
Allowautoredirect = false,//是否根据301跳转 可选项
ResultType = ResultType.String
};
result = http.GetHtml(item);
html = result.Html;
if (html.Contains("captchaservice"))
{
string codeurl = html.Substring(html.IndexOf("captchaservice"));
codeurl = codeurl.Remove(codeurl.IndexOf("&"));
item = new HttpItem()
{
URL = "https://passport.baidu.com/cgi-bin/genimage?" + codeurl,//URL 必需项
Encoding = System.Text.Encoding.GetEncoding("utf-8"),//URL 可选项 默认为Get
Method = "get",//URL 可选项 默认为Get
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = cookiestring,//字符串Cookie 可选项
Referer = "http://www.baidu.com/",//来源URL 可选项
Timeout = 100000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",
ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值
Allowautoredirect = false,//是否根据301跳转 可选项
ProxyIp = "",//代理服务器ID 可选项 不需要代理 时可以不设置这三个参数
ResultType = ResultType.Byte
};
result = http.GetHtml(item);
Image codeimg = byteArrayToImage(result.ResultByte);
code c = new code();
c.codeimg = codeimg;
c.cookie = cookiestring;
c.codeurl = codeurl;
c.ShowDialog();
item = new HttpItem()
{
URL = "http://passport.baidu.com/v2/api/?login",//URL 必需项
Encoding = System.Text.Encoding.GetEncoding("utf-8"),//URL 可选项 默认为Get
Method = "post",//URL 可选项 默认为Get
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = cookiestring,//字符串Cookie 可选项
Referer = "http://www.baidu.com/",//来源URL 可选项
Postdata = "staticpage=http%3A%2F%2Fhi.baidu.com%2Fcom%2Fshow%2Fproxy%3Ffor%3Dv3_pass_login&charset=utf-8&token=" + tooken + "&tpl=qing&apiver=v3&tt=1372661616626&codestring=" + codeurl + "&isPhone=false&safeflg=0&u=http%3A%2F%2Fhi.baidu.com%2F&username=shijiaxin3180&password=test123456&verifycode=" + c.codestr + "&mem_pass=on&ppui_logintime=165501&callback=parent.bd__pcbs__hggm3p",//Post数据 可选项GET时不需要写
Timeout = 100000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统 可选项有默认值
ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值
Allowautoredirect = false,//是否根据301跳转 可选项
ProxyIp = "",//代理服务器ID 可选项 不需要代理 时可以不设置这三个参数
ResultType = ResultType.String
};
result = http.GetHtml(item);
}
foreach (CookieItem s in HttpCookieHelper.GetCookieList(result.Cookie))
{
if (s.Key.Contains("BAIDUID") || s.Key.Contains("H_PS_PSSID") || s.Key.Contains("BDUSS"))
{
cookiestring = cookiestring + HttpCookieHelper.CookieFormat(s.Key, s.Value);
}
}
item = new HttpItem()
{
URL = "http://tieba.baidu.com/home/main?un=shijiaxin3180&fr=home",//URL 必需项
Encoding = System.Text.Encoding.GetEncoding("gb2312"),//URL 可选项 默认为Get
Method = "get",//URL 可选项 默认为Get
IsToLower = false,//得到的HTML代码是否转成小写 可选项默认转小写
Cookie = cookiestring,
//Cookie = "HOSUPPORT=1; BAIDUID=7E20E2535A9733EB2783FA1E1E90BAB8:FG=1;BDSVRTM=0;H_PS_PSSID=6192_6552_1425_5225_6995_6757_6506_7057_7038_4759_6017_6998_6930_6836_7051_6983_7107;BDUSS=VphWXlwRDBrLWh4c2JISFRsUWpNOW4xQ3BrYlJINDVOc0dTdlE3ZmFnT3B2OEpUQVFBQUFBJCQAAAAAAAAAAAEAAACGSXcKc2hpamlheGluMzE4MAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAKkym1OpMptTO;",//字符串Cookie 可选项
Referer = "http://www.baidu.com/",//来源URL 可选项
Postdata = "",//Post数据 可选项GET时不需要写
Timeout = 100000,//连接超时时间 可选项默认为100000
ReadWriteTimeout = 30000,//写入Post数据超时时间 可选项默认为30000
UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)",//用户的浏览器类型,版本,操作系统 可选项有默认值
ContentType = "application/x-www-form-urlencoded",//返回类型 可选项有默认值
Allowautoredirect = false,//是否根据301跳转 可选项
ResultType = ResultType.String
};
result = http.GetHtml(item);
t.tiebazhuyehtml = result.Html;
if (t.tiebazhuyehtml.Contains("我在百度"))
{
string jieguo="OK";
}
else
{
string jieguo="ERROR";
}