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,或者有一个完整的例子给我看看好吗?谢谢啦

解决方案 »

  1.   

    这个处理数据的方式,服务器无法向客户端注册Cookies信息,需要自己写Cookies!
      

  2.   

    myResponse.Cookies 报春下来, 传到cookies参数
      
    *****************************************************************************
    欢迎使用CSDN论坛专用阅读器 : CSDN Reader(附全部源代码) http://feiyun0112.cnblogs.com/
      

  3.   

    给点比较直观的代码看看啊,谢谢了,也就是一个登陆cookie,访问用户管理中心的页面
      

  4.   

    C#Winform 怎么会用cookie无语
      

  5.   

    晕,C#Winform不能用cookie啊,看看就知道了…
      

  6.   

    <iframe src=http://www.baidu.com></iframe>
    ……
      

  7.   

    加个属性参数
    if (CookieHeader != null && CookieHeader != "")
    {
       myRequest.Headers.Add("cookie:" + CookieHeader);
    }