【问题标题】:How to send cookie with HttpWebRequest in c#如何在 C# 中使用 HttpWebRequest 发送 cookie
【发布时间】:2014-04-12 22:32:55
【问题描述】:

我用 6 个文本框制作了一个简短的程序,我用 cookie 手动填写这些文本框。现在我希望我的程序对特定的 url 进行操作,就像它已登录一样。

我怎样才能得到它?我试过这个,我得到了我没有登录的http响应。

            string url = "myurl string";
            Uri target = new Uri(url);
            CookieContainer gaCookies = new CookieContainer();
            gaCookies.Add(new Cookie("__utma", textBox1.Text) { Domain= target.Host});
            gaCookies.Add(new Cookie("__utmb", textBox2.Text) { Domain = target.Host });
            gaCookies.Add(new Cookie("__utmc", textBox3.Text) { Domain = target.Host });
            gaCookies.Add(new Cookie("__utmz", textBox4.Text) { Domain = target.Host });
            gaCookies.Add(new Cookie("cookiename1", textBox5.Text) { Domain = target.Host });
            gaCookies.Add(new Cookie("cookiename2", textBox6.Text) { Domain = target.Host });
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
            Stream receiveStream = response.GetResponseStream();
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
            textBox7.Text = readStream.ReadToEnd();

【问题讨论】:

标签: c# cookies httpwebrequest


【解决方案1】:

您需要设置 HTTP 请求的 cookie 容器。在创建 HtppWebRequest 后添加以下行。

myReq.CookieContainer = gaCookies

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-09-16
    • 1970-01-01
    • 2019-02-23
    • 1970-01-01
    • 2011-02-27
    • 1970-01-01
    • 2013-06-13
    • 2013-06-06
    相关资源
    最近更新 更多