响应中的httpwebrequest cookie

时间:2018-05-03 12:45:35

标签: c# cookies httpwebrequest httpwebresponse

如何在新请求中使用响应中的Cookie?

所以基本上我在我的getresponse流中有一个if语句用于重定向,

代码示例 -

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (Stream stream2 = response.GetResponseStream())
        {
            if ((int)response.StatusCode >= 300 && (int)response.StatusCode <= 399)
            {   
                string newurl = "https://www.example.com/page2";

                request = request = (HttpWebRequest)WebRequest.Create(newurl);

            }
            using (StreamReader reader = new StreamReader(stream2, Encoding.UTF8))
            {
                str6 = reader.ReadToEnd();
            }
        }
        return str6;
    }

如何将响应Cookie /标头数据应用于我的新请求 -

            request = request = (HttpWebRequest)WebRequest.Create(newurl);

我知道我是否

response.Headers["Location"];

它会给我回复位置,但是饼干呢? &安培;我怎么能将这些cookie应用于请求

1 个答案:

答案 0 :(得分:0)

var myCookie = new HttpCookie("token");
myCookie.Value = Guid.NewGuid().ToString();
myCookie.Expires = DateTime.UtcNow.AddHours(10);
response.Cookies.Add(myCookie);