Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我无法从C#HTTPWebRequest获取cookie_C#_Http_Cookies_Request_Response - Fatal编程技术网

我无法从C#HTTPWebRequest获取cookie

我无法从C#HTTPWebRequest获取cookie,c#,http,cookies,request,response,C#,Http,Cookies,Request,Response,我需要从登录中获取cookie,因为我将在同一页面中应用此cookie。这是我的密码 string boundary = "41184676334"; string username = "username"; string password = "password"; string login_post_data = "POSTDATA=-----------------------------"

我需要从登录中获取cookie,因为我将在同一页面中应用此cookie。这是我的密码

        string boundary = "41184676334";
        string username = "username";
        string password = "password";


        string login_post_data =
               "POSTDATA=-----------------------------" + boundary  +
               "\nContent-Disposition: form-data; name=\"login\"" +
               "\n\n" + username  +
               "\n-----------------------------" + boundary  +
               "\nContent-Disposition: form-data; name=\"key\"" +
               "\n\n" + password  +
               "\n-----------------------------" + boundary  +
               "\nContent-Disposition: form-data; name=\"clcode\"" +
               "\n\n" +
               "\n-----------------------------" + boundary  + "--";

        var cookies = new CookieContainer();

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://site.com/cgi-bin/login.pl?logout");
        request.CookieContainer = cookies;



        request.Method = "POST";
        request.ContentType = "multipart/form-data; boundary=---------------------------" + boundary;

        request.Host = "site.com";
        request.UserAgent = "Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1";
        request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
        request.Referer = "https://site.com/cgi-bin/login.pl?logout";
        //request.Headers.Add("POSTDATA", post_data);

       using (var requestStream = request.GetRequestStream())
       {
           requestStream.Write(StrToByteArray(login_post_data), 0, StrToByteArray(login_post_data).Length);
        }



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



        using (Stream stream = response.GetResponseStream())
        {


            StreamReader reader = new StreamReader(stream);

            string temp = reader.ReadToEnd();
            richTextBox1.Text = temp;
        }


        foreach (string c in response.Cookies)
        {
            listBox1.Items.Add(c);
        }

问题是“https://site.com/cgi-bin/login.pl?logout“在我登录后是同一个页面,我需要传递cookie

看起来您试图在发送到URL的请求的同时登录和注销

https://site.com/cgi-bin/login.pl?logout
删除
?注销
参数,然后重试。请更新您的问题,如果它没有改变任何事情


请进一步解释您试图实现的目标,以便我们讨论代码是否正确。

在我输入用户名和密码并点击提交按钮后,url就是登录页面。地址栏上有相同的url,但页面布局不同。我想做的是拿到饼干。显然,我的代码没有起到作用。登录页面后的网页应该在其document.cookie中有一个cookie值,但它没有