关于HttpWebRequest模拟Web登陆和更新的WinForm程序问题,郁闷好几天了!请求大家帮忙.

DogBear_Zh 2006-10-26 02:38:39
最近在做一个WinForm程序,主要功能是实现自动登陆并且重定向到一个更新页面实现数据更新的操作.我写了一个函数,验证登陆是成功的,但是更新就不行了.代码如下:

public ErrorCode InsertSnippet(SiteID TheSiteID, string SiteLoginURL,string SiteInsertURL, string Username, string Password, int Timeout, int NumOfTries, string Snippet, ref string sMessage)
{
//Check the login
string str_path = SiteLoginURL;
string cookieheader = null;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(str_path);
HttpWebResponse response = null;
Stream dataStream = null;
request.Method = "POST"; //Set the Method property of the request to POST
request.ContentType = "application/x-www-form-urlencoded"; //Set the ContentType property of the webrequest
request.AllowAutoRedirect = true;
CookieContainer cookCT = new CookieContainer();
request.CookieContainer = cookCT;
string postdata = ""; //Create POST data and convert it to a byte array
postdata = "email=" + Username + "&password=" + Password + "&type=1"; //post data of the hi5.com site
byte[] byteArray = Encoding.UTF8.GetBytes(postdata);
request.ContentLength = byteArray.Length; //Set the ContentLength property of the WebRequest
request.Timeout = Timeout;
try
{
dataStream = request.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
response = (HttpWebResponse)request.GetResponse();//Gets the response information
cookieheader = request.CookieContainer.GetCookieHeader(new Uri(SiteLoginURL));
sMessage = response.StatusDescription;

}
catch (Exception e)
{
sMessage = e.Message.ToString();
throw (new Exception(e.Message.ToString(), e));
}

string PostData = null;
str_path = SiteInsertURL;
request = null;
request = (HttpWebRequest)HttpWebRequest.Create(SiteInsertURL);
response = null;
dataStream = null;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
CookieContainer newcc = new CookieContainer();
request.CookieContainer = newcc;
request.CookieContainer.SetCookies(new Uri(SiteInsertURL), cookieheader);
request.AllowAutoRedirect = true;
PostData = "timestamp=1161741302148&orientationStatus=&origOrientationStatus=&drinkerStatus=&origDrinkerStatus=&smokerStatus=&origSmokerStatus=&description="+Snippet.ToString()+"&matchDescription="; //post data to hi5.com site
byte[] ByteArray = Encoding.UTF8.GetBytes(PostData);
request.ContentLength = ByteArray.Length;
request.Timeout = Timeout;
try
{
dataStream = request.GetRequestStream();
dataStream.Write(ByteArray, 0, ByteArray.Length);
dataStream.Close();
response = (HttpWebResponse)request.GetResponse();
dataStream = response.GetResponseStream();
StreamReader sreader = new StreamReader(dataStream, ASCIIEncoding.UTF8);
string str = sreader.ReadToEnd();
sMessage = response.ResponseUri.AbsoluteUri;

}
catch (Exception e)
{
sMessage = e.Message.ToString();
throw (new Exception(e.Message.ToString(), e));
}
finally
{
dataStream.Close();
response.Close();
}
return ErrorCode.Success;
}
...全文
140 2 打赏 收藏 举报
写回复
2 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
MauveDragon 2006-12-07
  • 打赏
  • 举报
回复
服务器的应答呢?
DogBear_Zh 2006-10-26
  • 打赏
  • 举报
回复
我用IE提交的时候返回的相关信息如下:

POST /friend/account/editProfileLifestyle.do HTTP/1.1
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*
Referer: http://www.hi5.com/friend/account/displayEditProfileLifestyle.do
Accept-Language: zh-cn
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 1.1.4322)
Host: www.hi5.com
Content-Length: 166
Connection: Keep-Alive
Cache-Control: no-cache
Cookie: hi5photos=-1; WSSSampleCookie=N; JSESSIONID=aLOtcoddXRpf; LoginInfo=M_YA|CN_0_; Userid=100848631; sc=Ftnsr:1:Frsi:-1; hi5history=1161841671942/profile/displaySameProfile.do?userid=100848631!1161841683598/account/displayEditProfileCustomization.do!1161841686817/account/displayEditProfileLifestyle.do!1161841808319/account/editProfileLifestyle.do!1161843913799/account/editProfileLifestyle.do!1161843921471/displayMyProfile.do!1161843927174/displayEditProfile.do!1161843930768/account/displayEditProfileLifestyle.do; hi5sp=homepage; Email=dogbear_zh@hotmail.com; hi5history=; NSC_xxx-wjq=825e06d93660; hi5loggedIn=true

timestamp=1161843834702&orientationStatus=&origOrientationStatus=&drinkerStatus=&origDrinkerStatus=&smokerStatus=&origSmokerStatus=&description=test&matchDescription=GET /friend/js/forms/charcount.js HTTP/1.1
Accept: */*
Referer: http://www.hi5.com/friend/account/editProfileLifestyle.do
Accept-Language: zh-cn
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 2.0.50727; .NET CLR 1.1.4322)
Host: www.hi5.com
Connection: Keep-Alive
Cookie: hi5photos=-1; WSSSampleCookie=N; JSESSIONID=aLOtcoddXRpf; LoginInfo=M_YA|CN_0_; Userid=100848631; sc=Ftnsr:1:Frsi:-1; hi5history=1161841671942/profile/displaySameProfile.do?userid=100848631!1161841683598/account/displayEditProfileCustomization.do!1161841686817/account/displayEditProfileLifestyle.do!1161841808319/account/editProfileLifestyle.do!1161843913799/account/editProfileLifestyle.do!1161843921471/displayMyProfile.do!1161843927174/displayEditProfile.do!1161843930768/account/displayEditProfileLifestyle.do; hi5sp=homepage; Email=dogbear_zh@hotmail.com; hi5history=; NSC_xxx-wjq=825e06d93660; hi5loggedIn=true
相关推荐
发帖
C#

10.8w+

社区成员

.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
帖子事件
创建了帖子
2006-10-26 02:38
社区公告

让您成为最强悍的C#开发者