C#Winform登陆网站的问题

whowhen21 2009-07-08 04:19:14
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace WebLogin {
class WebServerClass {
public static HttpWebResponse CreateRequest(string url, string postData, CookieCollection cookies) {
Uri urls = new Uri(url);
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);

myRequest.Method = "POST";
myRequest.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-excel, application/msword, application/x-shockwave-flash, */*";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; .NET CLR 2.0.50727)";
myRequest.KeepAlive = true;
if(cookies != null) {
foreach(Cookie cook in cookies) {
cook.Domain = urls.Host;
myRequest.CookieContainer.Add(cook);
}
}

if(postData != null) {
ASCIIEncoding encoding = new ASCIIEncoding();
byte[] data = encoding.GetBytes(postData);
myRequest.ContentLength = data.Length;
Stream newStream = myRequest.GetRequestStream();
newStream.Write(data, 0, data.Length);
newStream.Close();
}

HttpWebResponse myResponse = (HttpWebResponse)myRequest.GetResponse();
return myResponse;
}

public static HttpWebResponse CreateRequest(string url) {
return CreateRequest(url, null, null);
}
}
}

这是我写的一个类,主要就是对网页数据的操作,然后我调用,把post表单数据提交,返回的一个页面,其中有正要跳转的页面,再通过正则表达式获取跳转的页面,然后用刚才登陆的用户来访问要跳转的页面就能够到达用户管理中心,可是我不知道如何处理cookie啊,再次访问跳转的页面时候,返回的确是请登录的那个页面啊,请教下,给点思路,关键是 处理cookie,或者有一个完整的例子给我看看好吗?谢谢啦
...全文
184 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
lianxw123 2010-08-12
  • 打赏
  • 举报
回复
来看看啦
beargo 2009-07-10
  • 打赏
  • 举报
回复
加个属性参数
if (CookieHeader != null && CookieHeader != "")
{
myRequest.Headers.Add("cookie:" + CookieHeader);
}
whowhen21 2009-07-09
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 createtim 的回复:]
C#Winform 怎么会用cookie无语
[/Quote]
晕,C#Winform不能用cookie啊,看看就知道了…
VistaKobe 2009-07-09
  • 打赏
  • 举报
回复
学习。
whowhen20 2009-07-09
  • 打赏
  • 举报
回复
看看~
whowhen20 2009-07-09
  • 打赏
  • 举报
回复
<iframe src=http://www.baidu.com></iframe>
……
createTim 2009-07-08
  • 打赏
  • 举报
回复
C#Winform 怎么会用cookie无语
skyctr 2009-07-08
  • 打赏
  • 举报
回复
学习了
whowhen21 2009-07-08
  • 打赏
  • 举报
回复
给点比较直观的代码看看啊,谢谢了,也就是一个登陆cookie,访问用户管理中心的页面
feiyun0112 2009-07-08
  • 打赏
  • 举报
回复
myResponse.Cookies 报春下来, 传到cookies参数

*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码)

http://feiyun0112.cnblogs.com/
悠索工作室 2009-07-08
  • 打赏
  • 举报
回复
这个处理数据的方式,服务器无法向客户端注册Cookies信息,需要自己写Cookies!

110,556

社区成员

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

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

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