C#无法创建https / ssl连接! [英] C# can't create https/ssl connection!

查看:89
本文介绍了C#无法创建https / ssl连接!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法通过C#标准webrequest建立https / ssl连接



我尝试过:



所以我使用这个代码,登录Steam网站使用已经制作的registerkey功能,所以我不需要手动操作,反正它一直在工作很长一段时间,但现在我不能使https / ssl连接,我不知道为什么?我有什么改变。我发送的发布数据没有错,当我将steampowered.com切换到steamcommunity.com时,cookie和它的工作也没有登录,但是我无法访问我需要的Steampowered.com部分,以及只有steampowered.com我不能发出https / ssl请求。 

< pre> LoginRequest(https://store.steampowered.com/login/getrsakey/,POST,postdata,cookies,headers);







 public string LoginRequest(string url,string method,NameValueCollection data = null,CookieContainer cookies = null,NameValueCollection headers = null,string referer = APIEndpoints.COMMUNITY_BASE,bool allowredirect = false,string host =)
{
string query =(data == null?string.Empty:string.Join(&,Array.ConvertAll(data.AllKeys,key => String.Format({0} = {1},WebUtility.UrlEncode(key),WebUtility.UrlEncode(data [key])))));
if(method ==GET)
{
url + =(url.Contains(?)?&:?)+ query;
}

HttpWebRequest request =(HttpWebRequest)WebRequest.Create(url);
request.Method = method;
request.Accept =text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp,image / apng,* / *; q = 0.8;
request.UserAgent =Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,与Gecko一样)Chrome / 67.0.3396.99 Safari / 537.36;
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
request.Referer = referer;
if(host.Length> 0)
{
request.Host = host;
}
if(allowredirect)
{
request.AllowAutoRedirect = true;
}
if(headers!= null)
{
request.Headers.Add(headers);
}

if(cookies!= null)
{
request.CookieContainer = cookies;
}

if(method ==POST)
{
request.ContentType =application / x-www-form-urlencoded; charset = UTF- 8\" ;
request.ContentLength = query.Length;

StreamWriter requestStream = new StreamWriter(request.GetRequestStream()); //在这里打破
requestStream.Write(query);
requestStream.Close();
}

尝试
{
使用(HttpWebResponse response =(HttpWebResponse)request.GetResponse())
{
if(response。 StatusCode!= HttpStatusCode.OK)
{
返回null;
}

使用(StreamReader responseStream = new StreamReader(response.GetResponseStream()))
{
string responseData = responseStream.ReadToEnd();
返回responseData;
}
}
}
catch(WebException)
{
返回null;
}
}

解决方案

该网站仅支持TLS 1.1或1.2。默认情况下,.NET仅使用SSL3或TLS 1.0。



您可以在应用程序启动时添加代码以启用TLS 1.1和1.2:

c# - 默认情况下为.NET 4.5和.NET 4.5.1启用TLS 1.1和TLS 1.2 - 堆栈溢出 [ ^ ]

 System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; 



或者,如果您使用的是.NET Framework 4.5或更高版本,则可以在运行代码的每台计算机上进行注册表更改,以便默认启用这些协议:

在.NET Framework 4.5或更高版本中启用强加密Microsoft Docs [ ^ ]

 Windows注册表编辑器版本5.00 

[HKEY_LOCAL_MACHINE \\SOFTWARE \\ Microros\\.NETFramework\\v4.0.30319]
SchUseStrongCrypto = dword:00000001

[HKEY_LOCAL_MACHINE \\SOFTWARE \\Wow6432Node\\Microsoft \\.NETFramework\\v4.0.30319]
SchUseStrongCrypto = DWORD:00000001


i cant make https/ssl connection with C# standard webrequest

What I have tried:

So i am using this code, to login on steam's website to use the "registerkey" function the have made so i dont need to do it manually, anyways it have been working long time now but now i cant make https/ssl connection  i dont know why? i have change anything. Post data i sent is not wrong and neither is the cookies and its work to make login when i switch "steampowered.com" to steamcommunity.com but then i can't access the  "Steampowered.com" part which i need, and its only to steampowered.com i cant make https/ssl request.

<pre> LoginRequest("https://store.steampowered.com/login/getrsakey/", "POST", postdata, cookies, headers);




public string LoginRequest(string url, string method, NameValueCollection data = null, CookieContainer cookies = null, NameValueCollection headers = null, string referer = APIEndpoints.COMMUNITY_BASE,bool allowredirect = false,string host = "")
            {
                string query = (data == null ? string.Empty : string.Join("&", Array.ConvertAll(data.AllKeys, key => String.Format("{0}={1}", WebUtility.UrlEncode(key), WebUtility.UrlEncode(data[key])))));
                if (method == "GET")
                {
                    url += (url.Contains("?") ? "&" : "?") + query;
                }

                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = method;
                request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8";
                request.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36";
                request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
                request.Referer = referer;
                if (host.Length > 0)
                {
                    request.Host = host;
                }
                if (allowredirect)
                {
                    request.AllowAutoRedirect = true;
                }
                if (headers != null)
                {
                    request.Headers.Add(headers);
                }

                if (cookies != null)
                {
                    request.CookieContainer = cookies;
                }

                if (method == "POST")
                {
                    request.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
                    request.ContentLength = query.Length;

                    StreamWriter requestStream = new StreamWriter(request.GetRequestStream()); //Breaks here
                    requestStream.Write(query);
                    requestStream.Close();
                }

                try
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                        {
                            return null;
                        }

                        using (StreamReader responseStream = new StreamReader(response.GetResponseStream()))
                        {
                            string responseData = responseStream.ReadToEnd();
                            return responseData;
                        }
                    }
                }
                catch (WebException)
                {
                    return null;
                }
            }

解决方案

That site only supports TLS 1.1 or 1.2. By default, .NET only uses SSL3 or TLS 1.0.

You can add code to your application's startup to enable TLS 1.1 and 1.2:
c# - Is TLS 1.1 and TLS 1.2 enabled by default for .NET 4.5 and .NET 4.5.1 - Stack Overflow[^]

System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;


Alternatively, if you are using .NET Framework 4.5 or higher, you can make registry changes on each machine where your code runs to enable these protocols by default:
Enable strong cryptography in .NET Framework 4.5 or higher | Microsoft Docs[^]

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\.NETFramework\\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001

[HKEY_LOCAL_MACHINE\\SOFTWARE\\Wow6432Node\\Microsoft\\.NETFramework\\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001


这篇关于C#无法创建https / ssl连接!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆