解决程序访问网页没有Cookie的问题。

本文介绍了如何在C#中使用HttpWebRequest和CookieContainer来获取和设置网站的Cookie,包括从响应中读取Cookie并将其传递到后续请求。
摘要由CSDN通过智能技术生成

 /*代码如下,获得Cookie,再发送过去*/

/*获取网站的Cook,cook储存在response.Cookies*/
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:52273/Default.aspx");
            request.CookieContainer = new CookieContainer();
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
           /*访问该网站,把获得的Cook再传过去*/
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:52273/Default.aspx");
            CookieContainer container = new CookieContainer();
            for(int i=0;i<response.Cookies.Count;i++)
            container.Add(response.Cookies[i]);
            req.CookieContainer = container;
            HttpWebResponse res = (HttpWebResponse)req.GetResponse();
            //读取网站里的所有数据
            Stream stream = res.GetResponseStream();
            StreamReader streamReader = new StreamReader(stream);
            String str = streamReader.ReadToEnd();
            MessageBox.Show(str);

以下是MSDN里获得网页Cookie的方法

usingSystem.Net;
usingSystem;
namespaceExamples.System.Net.Cookies
{
    // This example is run at the command line.
    // Specify one argument: the name of the host to 
    // send the request to.
    // If the request is sucessful, the example displays the contents of the cookies
    // returned by the host.

    publicclassCookieExample
    {   
        publicstaticvoidMain(string[] args)
        {   
            if(args == null|| args.Length != 1)
            {
                Console.WriteLine("Specify the URL to receive the request.");
                Environment.Exit(1);
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
            request.CookieContainer = newCookieContainer();

            HttpWebResponse response = (HttpWebResponse) request.GetResponse();



            // Print the properties of each cookie.
            foreach(Cookie cook inresponse.Cookies)
            {
                Console.WriteLine("Cookie:");
                Console.WriteLine("{0} = {1}", cook.Name, cook.Value);
                Console.WriteLine("Domain: {0}", cook.Domain);
                Console.WriteLine("Path: {0}", cook.Path);
                Console.WriteLine("Port: {0}", cook.Port);
                Console.WriteLine("Secure: {0}", cook.Secure);

                Console.WriteLine("When issued: {0}", cook.TimeStamp);
                Console.WriteLine("Expires: {0} (expired? {1})", 
                    cook.Expires, cook.Expired);
                Console.WriteLine("Don't save: {0}", cook.Discard);    
                Console.WriteLine("Comment: {0}", cook.Comment);
                Console.WriteLine("Uri for comments: {0}", cook.CommentUri);
                Console.WriteLine("Version: RFC {0}", cook.Version == 1 ? "2109": "2965");

                // Show the string representation of the cookie.
                Console.WriteLine ("String: {0}", cook.ToString());
            }
        }
    }
}
/*手动添加Cookie,然后发送给服务页面*/

         /*设置Cookie*/
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost:52273/Default.aspx");
            CookieContainer container = new CookieContainer();
            Cookie cook =new Cookie("ucel","verygood!");
            cook.Domain="localhost";
            container.Add(cook);
            request.CookieContainer = container;
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
       
            //读取网站里的所有数据
            Stream stream = response.GetResponseStream();
            StreamReader streamReader = new StreamReader(stream);
            String str = streamReader.ReadToEnd();
            MessageBox.Show(str);
            Environment.Exit(0);

上面Cookie里的Domain填的是网站服务器的主机名,可填IP地址。

  • 16
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bczheng1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值