• 主页
  • ASP
  • .NET Framework
  • Web Services
  • VB
  • VC
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • LINQ
sss190 2007年06月27日
自动填写表单功能,出现问题(HttpRequest和Weblient方式均试过)
代码如下(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);
...全文
127 点赞 收藏 7
写回复
7 条回复

还没有回复,快来抢沙发~

发动态
发帖子
.NET技术社区
创建于2007-09-28

4.8w+

社区成员

66.8w+

社区内容

.NET技术交流专区
社区公告
暂无公告