【发布时间】:2022-02-03 17:05:54
【问题描述】:
我目前正在尝试使用 RestSharp(版本 107.1.2)获取外部 API 的信息。不幸的是,在每个请求中,我都会收到响应状态代码 403“禁止”。我现在联系了提供商,他告诉我,先删除所有 cookie,然后添加 cookie“SMCHALLENGE=YES”。
我在 RestSharp 中尝试过,但是在使用 client.AddCookie 扩展时,我收到 ArgumentException。我现在找到了另一种选择,在标题中添加 cookie,但这也不起作用。
您有什么想法,如何删除所有 cookie,然后添加 SMCHALLENGE cookie?
var client = new RestClient("https://test.de/api/token");
string resource = null;
client.Authenticator = new HttpBasicAuthenticator("testUser", "testPW");
string apiKey = null;
var request = new RestRequest(resource, Method.Get);
//Following execution throws System.ArgumentException: "The {0} parameter cannot be an empty string. Parameter name: cookie.domain"
//client.AddCookie("SMCHALLENGE", "YES");
request.AddHeader("Cookie", "SMCHALLENGE=YES");
var response = client.ExecuteAsync(request);
response.Wait();
RestResponse rr = response.Result;
非常感谢!
【问题讨论】:
-
我现在走得更远了,现在通过 Postman 让它工作了。显然,一个名为“SMSESSION”的cookie是通过“Interceptor”提前检索到的。然后,该值作为 cookie 在请求中传递。不幸的是,我还不知道如何在 c# 中的请求之前读取和传递这个 SMSESSION cookie。
标签: c# cookies httpclient restsharp