我有个问题我用HttpWebRequest来实现自动登陆网站问题,总是登陆不了代码如下: IHTMLDocument2 doc = (IHTMLDocument2)Parse(html);
HTMLFormElement formEle = (HTMLFormElement)doc.forms.item(0, 0);
StringBuilder sb = new StringBuilder("__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&");
foreach (IHTMLElement ele in formEle)
{
if (ele.tagName.ToLower() == "input")
{
IHTMLInputElement iEle = (IHTMLInputElement)ele;
if (iEle.name.IndexOf("UserName") != -1)
{
sb.Append(HttpUtility.UrlEncode(iEle.name) + "=" + HttpUtility.UrlEncode("sq324044") + "&");
}
else if (iEle.name.IndexOf("Password") != -1)
{
sb.Append(HttpUtility.UrlEncode(iEle.name) + "=" + HttpUtility.UrlEncode("sakura88") + "&");
}
else
{
sb.Append(HttpUtility.UrlEncode(iEle.name) + "=" + HttpUtility.UrlEncode(iEle.value) + "&");
}
}
if (ele.tagName.ToLower() == "select")
{
IHTMLSelectElement sEle = (IHTMLSelectElement)ele;
sb.Append(HttpUtility.UrlEncode(sEle.name) + "=" + HttpUtility.UrlEncode(sEle.value) + "&");
}
} sb.Remove(sb.Length - 1, 1); ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(ValidateServerCertificate); ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(sb.ToString());
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://216.152.164.80/Members/LoggedInPage.aspx"); myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream(); // Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close(); // Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
string content = reader.ReadToEnd();
Console.WriteLine(content);
return content;
HTMLFormElement formEle = (HTMLFormElement)doc.forms.item(0, 0);
StringBuilder sb = new StringBuilder("__EVENTTARGET=&__EVENTARGUMENT=&__LASTFOCUS=&");
foreach (IHTMLElement ele in formEle)
{
if (ele.tagName.ToLower() == "input")
{
IHTMLInputElement iEle = (IHTMLInputElement)ele;
if (iEle.name.IndexOf("UserName") != -1)
{
sb.Append(HttpUtility.UrlEncode(iEle.name) + "=" + HttpUtility.UrlEncode("sq324044") + "&");
}
else if (iEle.name.IndexOf("Password") != -1)
{
sb.Append(HttpUtility.UrlEncode(iEle.name) + "=" + HttpUtility.UrlEncode("sakura88") + "&");
}
else
{
sb.Append(HttpUtility.UrlEncode(iEle.name) + "=" + HttpUtility.UrlEncode(iEle.value) + "&");
}
}
if (ele.tagName.ToLower() == "select")
{
IHTMLSelectElement sEle = (IHTMLSelectElement)ele;
sb.Append(HttpUtility.UrlEncode(sEle.name) + "=" + HttpUtility.UrlEncode(sEle.value) + "&");
}
} sb.Remove(sb.Length - 1, 1); ServicePointManager.ServerCertificateValidationCallback =
new RemoteCertificateValidationCallback(ValidateServerCertificate); ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(sb.ToString());
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create("https://216.152.164.80/Members/LoggedInPage.aspx"); myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream(); // Send the data.
newStream.Write(data, 0, data.Length);
newStream.Close(); // Get response
HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
StreamReader reader = new StreamReader(myResponse.GetResponseStream(), Encoding.Default);
string content = reader.ReadToEnd();
Console.WriteLine(content);
return content;
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货