简体   繁体   English

c# Httpclient 请求在 Windows 10 上工作正常返回 403 forbidden on windows 7(相同的代码)

[英]c# Httpclient request working fine on windows 10 returns 403 forbidden on windows 7 (same code)

var handler = new HttpClientHandler
{
    AllowAutoRedirect = false,
    AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate,
    UseCookies = true,
    CookieContainer = new CookieContainer(),
    UseProxy = false,
    SslProtocols = SslProtocols.Tls12
};

using var client = new HttpClient(handler, true)
{
    Timeout = TimeSpan.FromSeconds(15)
};

client.DefaultRequestHeaders.Add("Accept", "*/*");
client.DefaultRequestHeaders.Add("User-Agent", "API");

using var response = await client.PostAsync(websiteUrl, content, cancellationToken);

if (!response.IsSuccessStatusCode)
    return string.empty

return await response.Content.ReadAsStringAsync();

The above code works fine on any windows 10 machine I have tested it, but returns a forbidden 403 on windows 7.上面的代码在我测试过的任何 Windows 10 机器上都可以正常工作,但在 Windows 7 上返回禁止的 403。

Does anyone have any ideas?有没有人有任何想法? I have already tried UseDefaultCredentials = true on the handler's initializer but the behavior is still the same.我已经在处理程序的初始值设定项上尝试了 UseDefaultCredentials = true 但行为仍然相同。

The content is a simple string json content .内容是一个简单的字符串 json content

The 403 may be due to Windows 7 not supporting TLS by default: How to enable TLS 1.2 on clients 403 可能是由于 Windows 7 默认不支持 TLS: 如何在客户端上启用 TLS 1.2

Earlier versions of Windows, such as Windows 7 or Windows Server 2012, don't enable TLS 1.1 or TLS 1.2 by default for secure communications using WinHTTP.早期版本的 Windows(例如 Windows 7 或 Windows Server 2012)默认情况下不启用 TLS 1.1 或 TLS 1.2 以使用 WinHTTP 进行安全通信。

Check and see if you can enable TLS 1.2 on the Win 7 machine.检查并查看是否可以在 Win 7 机器上启用 TLS 1.2。 Or try removing the SSL transport all together:或者尝试一起删除 SSL 传输:

SslProtocols = SslProtocols.None

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

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