C# 我用饼干打电话给你

C# 我用饼干打电话给你,c#,postman,restsharp,restapi,C#,Postman,Restsharp,Restapi,使用RESTSharp,我可以登录: RestClient client = new RestClient(Constants.APIURL + "method/login"); CookieContainer cookieJar = new CookieContainer(); RestRequest request = new RestRequest(Method.POST); client.CookieContainer

使用RESTSharp,我可以登录:

RestClient client = new RestClient(Constants.APIURL + "method/login");
        CookieContainer cookieJar = new CookieContainer();
        RestRequest request = new RestRequest(Method.POST);
        client.CookieContainer = cookieJar;
        request.AddHeader("Content-Type", "application/json");
        request.AddHeader("Accept", "application/json");
        request.AddJsonBody(new
        {
            usr = username,
            pwd = password
        });

        var response = client.Execute(request);
        var cookie = HttpContext.Current.Server.UrlDecode(response.Headers.ToList().Find(x => x.Name == "Set-Cookie").Value.ToString());
然后我存储cookie并发送到另一个API调用,也通过RESTSharp

RestClient client = new RestClient(Constants.APIURL);
        RestRequest request = new RestRequest("resource/Asset", Method.GET);
        request.AddCookie("Cookie", HttpContext.Current.Server.UrlEncode(cookie));
但它不断返回403禁止。我试过邮递员,效果非常好


有什么帮助吗?是我把饼干送错了吗?我试着在一个
HttpWebRequest
中发送cookie,它工作得非常好。

我还试着复制粘贴一个从邮递员那里生成的代码,其中cookie在头中传递,但它不起作用。我试着发送cookie,如下所示,效果很好


client.AddDefaultHeader(“Cookie”,Cookie)

您确定
Cookie
是Cookie的正确名称吗?也许服务器希望cookie被调用,比如
授权
我从POSTMAN复制了RESTSharp生成的代码。