代码如下(httprequest方式):
HttpWebResponse res = null;
string strResult = "";
if (HttpContext.Current.Application["cookieheader"] != null)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://et.szhome.com/LoginPage.aspx");
req.Method = "POST";
req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*";
req.Referer = "http://et.szhome.com";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)";
req.CookieContainer = (CookieContainer)(HttpContext.Current.Application["cookieheader"]);
// byte[] SomeBytes = Encoding.UTF8.GetBytes(HttpUtility.UrlEncode("UserName=szcentaline&Password=000000").ToString());
byte[] SomeBytes = Encoding.GetEncoding("GB2312").GetBytes(HttpUtility.UrlEncode("UserName=szcentaline&Password=000000").ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes,0,SomeBytes.Length);
newStream.Close();
res = (HttpWebResponse)req.GetResponse();
}
else
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://et.szhome.com/LoginPage.aspx"); //构造请求并保存cookie
req.Method = "POST";
req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*";
req.Referer = "http://et.szhome.com";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)";
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
// byte[] SomeBytes = Encoding.UTF8.GetBytes(HttpUtility.UrlEncode("UserName=szcentaline&Password=000000").ToString());
byte[] SomeBytes = Encoding.GetEncoding("GB2312").GetBytes(HttpUtility.UrlEncode("UserName=szcental&Password=123456").ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
res = (HttpWebResponse)req.GetResponse();
cookieheader = req.CookieContainer.GetCookieHeader(new Uri("http://et.szhome.com/LoginPage.aspx"));
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["cookieheader"] = cookieheader;
HttpContext.Current.Application.UnLock();
}
Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
StreamReader sr = new StreamReader(ReceiveStream,encode);
Char[] read = new Char[256];
int count = sr.Read(read,0,256);
while(count > 0)
{
String str = new String(read,0,count);
strResult += str;
count = sr.Read(read,0,256);
}
FileStream htmFile = new FileStream("D:\\save.htm",FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(htmFile);
sw.Write(strResult);
sw.Close();
htmFile.Close();
Response.Write(strResult);
HttpWebResponse res = null;
string strResult = "";
if (HttpContext.Current.Application["cookieheader"] != null)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://et.szhome.com/LoginPage.aspx");
req.Method = "POST";
req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*";
req.Referer = "http://et.szhome.com";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)";
req.CookieContainer = (CookieContainer)(HttpContext.Current.Application["cookieheader"]);
// byte[] SomeBytes = Encoding.UTF8.GetBytes(HttpUtility.UrlEncode("UserName=szcentaline&Password=000000").ToString());
byte[] SomeBytes = Encoding.GetEncoding("GB2312").GetBytes(HttpUtility.UrlEncode("UserName=szcentaline&Password=000000").ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes,0,SomeBytes.Length);
newStream.Close();
res = (HttpWebResponse)req.GetResponse();
}
else
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://et.szhome.com/LoginPage.aspx"); //构造请求并保存cookie
req.Method = "POST";
req.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*";
req.Referer = "http://et.szhome.com";
req.ContentType = "application/x-www-form-urlencoded";
req.UserAgent="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; .NET CLR 1.0.3705)";
CookieContainer cookieCon = new CookieContainer();
req.CookieContainer = cookieCon;
// byte[] SomeBytes = Encoding.UTF8.GetBytes(HttpUtility.UrlEncode("UserName=szcentaline&Password=000000").ToString());
byte[] SomeBytes = Encoding.GetEncoding("GB2312").GetBytes(HttpUtility.UrlEncode("UserName=szcental&Password=123456").ToString());
req.ContentLength = SomeBytes.Length;
Stream newStream = req.GetRequestStream();
newStream.Write(SomeBytes, 0, SomeBytes.Length);
newStream.Close();
res = (HttpWebResponse)req.GetResponse();
cookieheader = req.CookieContainer.GetCookieHeader(new Uri("http://et.szhome.com/LoginPage.aspx"));
HttpContext.Current.Application.Lock();
HttpContext.Current.Application["cookieheader"] = cookieheader;
HttpContext.Current.Application.UnLock();
}
Stream ReceiveStream = res.GetResponseStream();
Encoding encode = System.Text.Encoding.GetEncoding("GB2312");
StreamReader sr = new StreamReader(ReceiveStream,encode);
Char[] read = new Char[256];
int count = sr.Read(read,0,256);
while(count > 0)
{
String str = new String(read,0,count);
strResult += str;
count = sr.Read(read,0,256);
}
FileStream htmFile = new FileStream("D:\\save.htm",FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(htmFile);
sw.Write(strResult);
sw.Close();
htmFile.Close();
Response.Write(strResult);
"
对于其他asp或jsp的也许可以,但asp.net不行.asp通过viewstate管理页面状态的. 所以你要把这个登录页面所有<form中的元素提交回去.
包括hidden
string postString = "ctl00$ContentPlace$UserName=szcentaline&ctl00$ContentPlace$Password=000000" + "&ctl00$ContentPlace$LoginButton=" + submitButton + "&__VIEWSTATE=" + viewState + "&__EVENTVALIDATION=" + eventValidation;但是仍然无法登陆,是不是还少东西呢