如何在 .net 中使用 HttpClient 启用 cookie 来处理 Cloudflare 的 403 状态代码和“请启用 cookie。”?

问题描述 投票:0回答:1

我已经使用 GetSling.com 的 API 好几年了,但最近他们进行了一些更改,这让生活变得更加困难。我正在尝试使用下面的 C# .Net 代码将用户登录到 API。

起初响应返回了状态码 403 Forbidden 和一条消息“不支持的网络浏览器,请使用其他浏览器”,所以我添加了

    client.DefaultRequestHeaders.Add("User-Agent", "Other/0.0.1");

这让它变得更快乐了一点。

但现在显示的是“请启用cookie。”。我尝试通过将 CookieContainer 添加到 HttpClientHandler 和 HttpClient 来启用 cookie。 403 Forbidden 状态代码仍然存在。显然,GetSling.com 现在正在使用 Cloudflare 服务,该服务是通过使用 .Net 访问其 API 来触发的。如果我使用curl 使用相同的命中,效果会很好。

    curl -X POST "https://api.getsling.com/v1/account/login" -H "Content-Type: application/json" -d "{\"email\":\"[email protected]\",\"password\":\"xxxx\"}"

完整错误消息的文本:

请启用cookie。

您无法访问 api.getsling.com

为什么我被屏蔽了?

本网站使用安全服务来保护自己免受在线攻击。您刚刚执行的操作触发了安全解决方案。有多种操作可能会触发此阻止,包括提交特定单词或短语、SQL 命令或格式错误的数据。

我可以做什么来解决这个问题?

您可以向网站所有者发送电子邮件,让他们知道您已被屏蔽。请注明出现此页面时您正在做什么以及此页面底部找到的 Cloudflare Ray ID。

这是我尝试用来启用 cookie 并访问帐户/登录 API 的 C# .Net 代码。

    string Json = string.Format("{{\"email\":\"{0}\",\"password\":\"{1}\"}}",
        Email.Trim(),
        Password.Trim());
    StringContent Content = new StringContent(Json);
    Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
    
    try
    {
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = 
            SecurityProtocolType.Ssl3 | 
            SecurityProtocolType.Tls | 
            SecurityProtocolType.Tls11 | 
            SecurityProtocolType.Tls12;
    
        CookieContainer cookies = new CookieContainer();
        HttpClientHandler clientHandler = new HttpClientHandler();
        clientHandler.CookieContainer = cookies;
        clientHandler.UseCookies = true;
    
        HttpClient client = new HttpClient(clientHandler);
        client.DefaultRequestHeaders.Add("User-Agent", "Other/0.0.1");                
        client.DefaultRequestHeaders.Add("Accept", "application/json");
        client.BaseAddress = new Uri("https://api.getsling.com");
        HttpResponseMessage Response = await client.PostAsync("/v1/account/login", Content);
    }

我将尝试按照错误页面文本中的建议,使用 Cloudflare Ray ID 向 GetSling.com 发送电子邮件,但我怀疑这会走得很远。

其他人也遇到过类似的事情吗?

谢谢, 拉斯

c# .net cookies httpclient cloudflare
1个回答
0
投票

我在 Cloudflare 中看到了这种错误,就我而言,这是托管挑战配置过于严格的问题:https://developers.cloudflare.com/firewall/cf-firewall-rules/cloudflare-challenges/#cloudflare -挑战

因为 Cloudflare 中的域在我的控制之下,所以我更新了规则,仅此而已。

由于我们不知道规则是如何配置的,因此我们无法对此做任何事情,只能等待支持人员的答复。

您也可以查看此建议,也许其中的一些内容会对您有所帮助: https://developers.cloudflare.com/fundamentals/getstarted/concepts/cloudflare-challenges/

© www.soinside.com 2019 - 2024. All rights reserved.