代码如下(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);

解决方案 »

  1.   

    报错到没有.整个程序也全部执行了,但是最后生成的字符串(html标签集合)中,action那里始终是action="LoginPage.aspx?ReturnUrl=%2fManage%2fDefault.aspx",即没有登陆成功.
    "
      

  2.   

    你只送了username和password这是不对的.
     对于其他asp或jsp的也许可以,但asp.net不行.asp通过viewstate管理页面状态的. 所以你要把这个登录页面所有<form中的元素提交回去.
    包括hidden
      

  3.   

    我试过,最早提交的是:
    string postString = "ctl00$ContentPlace$UserName=szcentaline&ctl00$ContentPlace$Password=000000" + "&ctl00$ContentPlace$LoginButton=" + submitButton + "&__VIEWSTATE=" + viewState + "&__EVENTVALIDATION=" + eventValidation;但是仍然无法登陆,是不是还少东西呢