简体   繁体   English

C# RestSharp Cookies

[英]C# RestSharp Cookies

So Im learning RestSharp所以我正在学习 RestSharp

But I'm stuck at this problem which is getting specific string for client cookies here is my code:但我被困在这个问题上,它正在为客户端 cookies 获取特定的字符串,这是我的代码:

 var cookieJar = new CookieContainer();
        var client = new RestClient("https://server.com")
        {
            UserAgent =
                       "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36",



        };
        client.CookieContainer = cookieJar;
        var request = new RestRequest(Method.GET);
            var cookie = client.CookieContainer.GetCookieHeader(new Uri("https://server.com"));
        MessageBox.Show(""+cookie);

and I always get the cookie empty can anyone helps me?我总是把饼干弄空有人能帮我吗?

This will set the cookie for your client.这将为您的客户设置 cookie。 After all, you need to do is client.Execute .毕竟,你需要做的是client.Execute The code is in C# pretty sure you can make it work for anything else.代码在 C# 中,您可以肯定它可以用于其他任何东西。

string myUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36";

client.CookieContainer.Add(new Cookie("UserAgent", myUserAgent) { Domain = myUri.Host });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM