Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.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#HttpRequest将Cookie附加到请求_C#_Cookies_Httprequest - Fatal编程技术网

C#HttpRequest将Cookie附加到请求

C#HttpRequest将Cookie附加到请求,c#,cookies,httprequest,C#,Cookies,Httprequest,我想发送一个从Google Chromes network F12选项卡中提取的cookie,以在HTTP请求中预制作对UI的点击。但是,我需要发送请求附带的cookie。我似乎无法将cookie字符串附加到请求 从Google Chrome(f12)捕获 我需要在发送请求时将该cookie附加到请求 string cookie = "JSESSIONID_loginLite_remote=LJyKVLHGLZrpTq3npBWh8CJyqTND32HpscL48ndnSp0h

我想发送一个从Google Chromes network F12选项卡中提取的cookie,以在HTTP请求中预制作对UI的点击。但是,我需要发送请求附带的cookie。我似乎无法将cookie字符串附加到请求

从Google Chrome(f12)捕获

我需要在发送请求时将该cookie附加到请求

        string cookie = "JSESSIONID_loginLite_remote=LJyKVLHGLZrpTq3npBWh8CJyqTND32HpscL48ndnSp0hj2g6yhcK!.....";
        string info = "RemoteObjectTimestamp=1430842283795&RemoteObjectRequest=%7B%22destination%22%...";

        char[] arr      = info.ToCharArray(); 
        byte[] data     = System.Text.Encoding.ASCII.GetBytes(arr);

        HttpWebRequest myHttpWebRequest = (HttpWebRequest)HttpWebRequest.Create(URL);
        myHttpWebRequest.Method         = "POST";
        myHttpWebRequest.ContentType    = "application/x-www-form-urlencoded;charset=UTF-8";
        myHttpWebRequest.ContentLength  = data.Length;

        //Data to write is not empty
        if (data.Length > 0)
        {
            Stream requestStream = myHttpWebRequest.GetRequestStream();
            requestStream.Write(data, 0, data.Length);
            requestStream.Close();
        }

        HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();

        Stream responseStream = myHttpWebResponse.GetResponseStream();
        StreamReader myStreamReader = new StreamReader(responseStream, System.Text.Encoding.Default);
        string pageContent = myStreamReader.ReadToEnd();

        myStreamReader.Close();
        responseStream.Close();
        myHttpWebResponse.Close();

        Console.WriteLine(pageContent);
添加以下行:

myHttpWebRequest.CookieContainer = new CookieContainer();  
myHttpWebRequest.CookieContainer.SetCookies(new Uri("your domain here"), cookie);

别忘了将“您的域”替换为目标请求Uri的域

我没有cookie。我有一根巨大的绳子,里面有很多部分。所以我需要把这个巨大的字符串转换成一个cookie,然后我可以把它添加到请求中。你有没有试过在我上面的方法中把这个巨大的字符串放在“cookie”变量中?这是个问题吗?