C#/Asp.net 如何用HttpWebRequest Post自动登录、填表提交?

joeguonet 2011-10-12 10:31:08
CookieContainer RequestCookies = new CookieContainer();
protected void Page_Load(object sender, EventArgs e)
{
string username = "zhanghao";
string pwd = "mima";
string posturl = "http://my.anjuke.com/v2/my/login";
string postdata = "loginpost=1&formhash=&history=aHR0cDovL3NoYW5naGFpLmFuanVrZS5jb20vdjIv&systemtime=1297321220&loginName=" + username + "&loginPasswd=" + pwd + "&remember=1&submit=";
getMcc(posturl, postdata);

postdata = "txtCommunity=德安大厦&radUseType=5&txtProFloor=1&txtFloorNum=2&txtAreaNum=30&txtRoomNum=1&txtHallNum=0&txtToiletNum=0&txtProPrice=50&txtHouseAge=2000&radFitment=1&radHouseOri=0&radCurrentState=1&txtUserDefine=zheshibeizhu&txtProName=zheshibiaoti&areaTplId=&commTplId=&txtExplain=<p>zheshimiaoshu</p>&radSaleType=2&hidact=save&hidBrokerID=667389&hidCommunityID=3726";
posturl = "http://my.anjuke.com/v2/user/broker/property/sale/step1";
getMcc(posturl, postdata);
}


public void getMcc(string PostUrl, string PostData)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(PostUrl);

if (!string.IsNullOrEmpty(PostData))
{
request.CookieContainer = RequestCookies;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)";

Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8");
byte[] data = encoding.GetBytes(PostData);
request.ContentLength = data.Length;

Stream newStream = request.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream stream = response.GetResponseStream();

using (StreamReader read = new StreamReader(stream, Encoding.UTF8))
{
string strcontent = read.ReadToEnd();
}
}


我的代码如上 但是登录完成后提交发布的时候不行 怎么回事啊? 求教各位大侠!~~~~~~~
...全文
86 1 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
joeguonet 2011-10-12
  • 打赏
  • 举报
回复
各位前辈请帮忙指导下啊~~~~ 谢谢了~~~~

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

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

试试用AI创作助手写篇文章吧