Click here to Skip to main content
15,922,584 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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;
                }
            }
Posted
Updated 27-Jul-18 6:37am
v2
Comments
Richard MacCutchan 27-Jul-18 12:09pm    
It sounds like something has changed in that website. Contact their administrator for assistance.

1 solution

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[^]
C#
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
 
Share this answer
 
Comments
wawada 27-Jul-18 14:55pm    
Im gonna test it. it worked before to do ssl connection but i guess they changed

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900