110,578
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 获取指定页面的HTML代码
/// </summary>
/// <param name="url">指定页面的路径</param>
/// <param name="postData">回发的数据</param>
/// <param name="isPost">是否以post方式发送请求</param>
/// <param name="cookieCollection">Cookie集合</param>
/// <returns></returns>
public string GetHtml(string url, string postData, bool isPost, CookieContainer cookieContainer)
{
if (string.IsNullOrEmpty(postData))
{
return GetHtml(url, cookieContainer);
}
Thread.Sleep(NetworkDelay);
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;
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;
return string.Empty;
}
}
/// <summary>
/// 获取指定页面的HTML代码
/// </summary>
/// <param name="url">指定页面的路径</param>
/// <param name="cookieCollection">Cookie集合</param>
/// <returns></returns>
public string 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;
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;
return string.Empty;
}
}
/// <summary>
/// 登录
/// </summary>
/// <param name="loginEmail">用户名</param>
/// <param name="loginPassword">密码</param>
/// <returns></returns>
public bool Login(string username, string userPWD, string strTemp, System.Windows.Forms.HtmlDocument doc, out string errorTxt)
{
HttpHelper httpHelper = new HttpHelper();
string strRetVal = "";
HtmlDocument doc1 = doc;
if (doc1 == null)
{
errorTxt = "加载必要控件失败,请重新开启软件";
return false;
}
strRetVal = (string)doc1.InvokeScript("md5_3", new object[] { userPWD });
strRetVal += strTemp.ToUpper();
strRetVal = (string)doc1.InvokeScript("md5", new object[] { strRetVal });
string postData = "u=" + username + "&p=" + strRetVal + "&verifycode=" + strTemp + "&aid=15000101&u1=http%3A%2F%2Fphp.qzone.qq.com%2Findex.php%3Fmod%3Dportal%26act%3Dlogin&fp=loginerroralert&h=1&ptredirect=1&ptlang=0&from_ui=1&dumy=";
string result = httpHelper.GetHtml("http://ptlogin2.qq.com/login", postData, true, cookie);
errorTxt = result;
result = httpHelper.GetHtml("http://php.qzone.qq.com/index.php?mod=portal&act=login", cookie);
bool isLogin = result.Contains("g_iLoginUin = " + username);
if (!isLogin)
{
if (result.Contains("完成跳转"))
{
isLogin = true;
}
if (!isLogin)
{
isLogin = result.Contains("g_iLoginUin=" + username);
}
}
return isLogin;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>QQ社区登录</title>
<style>
body{ font:"宋体",Arial, Helvetica, sans-serif; font-size:14px; line-height:25px;}
.wrong{ width:auto; margin:0 auto; padding:20px; background:#F0F0F0; border:#CCCCCC solid 1px;}
h2{ color: #FF0000 ; font-size:16px; }
.STYLE1 {color: #FF3300}
</style>
<script language="javascript">
document.domain="qq.com"
function timeelapse(){
top.location.href='http://php.qzone.qq.com/index.php?mod=portal&act=login';
}
timeelapse();
</script>
</head>
<body>
<div class="wrong" >
如果未能自动跳转,请<a href="http://php.qzone.qq.com/index.php?mod=portal&act=login" target="_top">点击</a>完成跳转。</div>
</body>
</html>