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,或者有一个完整的例子给我看看好吗?谢谢啦
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,或者有一个完整的例子给我看看好吗?谢谢啦
*****************************************************************************
欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
……
if (CookieHeader != null && CookieHeader != "")
{
myRequest.Headers.Add("cookie:" + CookieHeader);
}