自动填写表单功能,出现问题(HttpRequest和Weblient方式均试过)

sss190 2007-06-27 10:35:20
代码如下(httprequest方式):
HttpWebResponse res = null;
string strResult = "";
if (HttpContext.Current.Application["cookieheader"] != null)
{
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://market.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://market.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://market.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://market.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://market.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);
...全文
235 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
cpp2017 2007-06-27
  • 打赏
  • 举报
回复
肯定少,再检查
sss190 2007-06-27
  • 打赏
  • 举报
回复
我试过,最早提交的是:
string postString = "ctl00$ContentPlace$UserName=szcentaline&ctl00$ContentPlace$Password=000000" + "&ctl00$ContentPlace$LoginButton=" + submitButton + "&__VIEWSTATE=" + viewState + "&__EVENTVALIDATION=" + eventValidation;

但是仍然无法登陆,是不是还少东西呢
cpp2017 2007-06-27
  • 打赏
  • 举报
回复
你只送了username和password这是不对的. 对于其他asp或jsp的也许可以,但asp.net不行.asp通过viewstate管理页面状态的. 所以你要把这个登录页面所有<form中的元素提交回去. 包括hidden
sss190 2007-06-27
  • 打赏
  • 举报
回复
报错到没有.整个程序也全部执行了,但是最后生成的字符串(html标签集合)中,action那里始终是action="LoginPage.aspx?ReturnUrl=%2fManage%2fDefault.aspx",即没有登陆成功.
"
lee_wei 2007-06-27
  • 打赏
  • 举报
回复
看得我头晕....
cpp2017 2007-06-27
  • 打赏
  • 举报
回复
报什么错?程序运行到哪里了?现象是什么?服务器端有没有跟踪?
sss190 2007-06-27
  • 打赏
  • 举报
回复
不知道哪里出现的问题,总之无法自动提交表单,请指教

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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