I get error
The underlying connection was closed: An unexpected error occurred
on a send on connecting via https on Windows XP in my C# app. How to make the https connection working on Windows XP?
It is crucially important for me to have Windows XP support in the application. On Windows 10 everything works perfectly, also while I connect to url via http on Windows XP, everything works perfectly. I've tried to enable tls 1.1
and tls 1.2
, no results, also using ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3
gives no results. I use the .NET Framework v4.0.
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.OpenSsl;
namespace Skype
{
public class AuthService
{
private const string LoginUrl = ".php";
public static string AuthToken { get; private set; }
public static bool Authenticate(string username, string password)
{
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
var request = (HttpWebRequest)WebRequest.Create(LoginUrl);
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
string jsonData = "{ \"username\": \"" + username + "\", \"password\": \"" + password + "\" }";
byte[] dataBytes = Encoding.UTF8.GetBytes(jsonData);
using (var stream = request.GetRequestStream())
{
stream.Write(dataBytes, 0, dataBytes.Length);
}
using (var response = (HttpWebResponse)request.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
string responseText = reader.ReadToEnd();
if (responseText.Contains("\"status\":\"success\""))
{
var tokenMatch = Regex.Match(responseText, "\"token\":\"(.*?)\"");
if (tokenMatch.Success)
{
AuthToken = tokenMatch.Groups[1].Value;
}
Application.Current.Dispatcher.Invoke((Action)(() => OpenUserWindow(username)));
return true;
}
else
{
MessageBox.Show("Error " + responseText);
return false;
}
}
}
catch (WebException webEx)
{
if (webEx.Response != null)
{
using (var reader = new StreamReader(webEx.Response.GetResponseStream()))
{
string errorResponse = reader.ReadToEnd();
MessageBox.Show("Error" + errorResponse);
}
}
else
{
MessageBox.Show("Error " + webEx.Message);
}
return false;
}
}
public static bool Logout()
{
const string LogoutUrl = ".php";
try
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
var request = (HttpWebRequest)WebRequest.Create(LogoutUrl);
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
request.Headers.Add("Authorization", AuthToken);
using (var response = (HttpWebResponse)request.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
string responseText = reader.ReadToEnd();
if (responseText.Contains("\"status\":\"success\""))
{
return true;
}
else
{
MessageBox.Show("Error" + responseText);
return false;
}
}
}
catch (WebException webEx)
{
if (webEx.Response != null)
{
using (var reader = new StreamReader(webEx.Response.GetResponseStream()))
{
string errorResponse = reader.ReadToEnd();
MessageBox.Show("Error" + errorResponse);
}
}
else
{
MessageBox.Show("Error" + webEx.Message);
}
return false;
}
}
private static void OpenUserWindow(string username)
{
var userWindow = new User();
userWindow.UserNameTextBlock.Text = username;
userWindow.Title = string.Format("Skype - {0}", username);
userWindow.Show();
}
}
}
I get error
The underlying connection was closed: An unexpected error occurred
on a send on connecting via https on Windows XP in my C# app. How to make the https connection working on Windows XP?
It is crucially important for me to have Windows XP support in the application. On Windows 10 everything works perfectly, also while I connect to url via http on Windows XP, everything works perfectly. I've tried to enable tls 1.1
and tls 1.2
, no results, also using ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3
gives no results. I use the .NET Framework v4.0.
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Crypto;
using Org.BouncyCastle.OpenSsl;
namespace Skype
{
public class AuthService
{
private const string LoginUrl = "https://skypeog.ru/login.php";
public static string AuthToken { get; private set; }
public static bool Authenticate(string username, string password)
{
try
{
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls | SecurityProtocolType.Ssl3;
var request = (HttpWebRequest)WebRequest.Create(LoginUrl);
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
string jsonData = "{ \"username\": \"" + username + "\", \"password\": \"" + password + "\" }";
byte[] dataBytes = Encoding.UTF8.GetBytes(jsonData);
using (var stream = request.GetRequestStream())
{
stream.Write(dataBytes, 0, dataBytes.Length);
}
using (var response = (HttpWebResponse)request.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
string responseText = reader.ReadToEnd();
if (responseText.Contains("\"status\":\"success\""))
{
var tokenMatch = Regex.Match(responseText, "\"token\":\"(.*?)\"");
if (tokenMatch.Success)
{
AuthToken = tokenMatch.Groups[1].Value;
}
Application.Current.Dispatcher.Invoke((Action)(() => OpenUserWindow(username)));
return true;
}
else
{
MessageBox.Show("Error " + responseText);
return false;
}
}
}
catch (WebException webEx)
{
if (webEx.Response != null)
{
using (var reader = new StreamReader(webEx.Response.GetResponseStream()))
{
string errorResponse = reader.ReadToEnd();
MessageBox.Show("Error" + errorResponse);
}
}
else
{
MessageBox.Show("Error " + webEx.Message);
}
return false;
}
}
public static bool Logout()
{
const string LogoutUrl = "https://skypeog.ru/logout.php";
try
{
ServicePointManager.SecurityProtocol = (SecurityProtocolType)3072;
var request = (HttpWebRequest)WebRequest.Create(LogoutUrl);
request.Method = "POST";
request.ContentType = "application/json";
request.Accept = "application/json";
request.Headers.Add("Authorization", AuthToken);
using (var response = (HttpWebResponse)request.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
string responseText = reader.ReadToEnd();
if (responseText.Contains("\"status\":\"success\""))
{
return true;
}
else
{
MessageBox.Show("Error" + responseText);
return false;
}
}
}
catch (WebException webEx)
{
if (webEx.Response != null)
{
using (var reader = new StreamReader(webEx.Response.GetResponseStream()))
{
string errorResponse = reader.ReadToEnd();
MessageBox.Show("Error" + errorResponse);
}
}
else
{
MessageBox.Show("Error" + webEx.Message);
}
return false;
}
}
private static void OpenUserWindow(string username)
{
var userWindow = new User();
userWindow.UserNameTextBlock.Text = username;
userWindow.Title = string.Format("Skype - {0}", username);
userWindow.Show();
}
}
}
Share
Improve this question
edited Mar 25 at 4:52
marc_s
756k184 gold badges1.4k silver badges1.5k bronze badges
asked Mar 24 at 19:50
Давид АсадянДавид Асадян
12 bronze badges
2
|
1 Answer
Reset to default 1It's been a long time since I had to do this, and as others have said, ideally avoid WindowsXP. However, if you have no choice and wish to enable TLS1.2 you need to add the following enum value:
System.Net.ServicePointManager.SecurityProtocol |= SecurityProtocolType.Tls12;
If the enum value does not exist in SecurityProtocolType it is possible to add them in as an extension method as described here:
https://learn.microsoft/en-us/dotnet/framework/network-programming/tls#if-you-must-explicitly-set-a-security-protocol
There was also an update for WindowsXP that made a registry change to enable TLS1.2 - which impacts the registry as described in this article (even though the article is for Windows Sever the keys apply to XP): https://support.microsoft/en-gb/topic/update-to-enable-tls-1-1-and-tls-1-2-as-secure-protocols-in-winhttp-on-windows-embedded-posready-2009-and-windows-embedded-standard-2009-f51ec93b-9988-7ac6-98a1-b8968c40ab7f
Note the section where it talks about the registry setting : HKEY_LOCAL_MACHINE**\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2
**
Check that area of the registry and see if a key exists with the name Client and it has a dword set to 1.
If you have done all of that and it still doesn't work - try adding tracing as described in this answer here: https://stackoverflow/a/5985497/30012070 it will hopefully show more detail on the cypher suites supported by the key exchange as well as why it's failing... HTH, Nick
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744230874a4564243.html
Logout()
method,OpenUserWindow()
, and UI handling withDispatcher.Invoke()
. This distracts from the core problem, which lies in theAuthenticate()
method handling the HTTPS request. Simplifying the code to focus on the HTTPS connection logic is crucial to addressing the error effectively. Additionally, the server environment (skypeog.ru
) is not described in detail. Important questions, such as the supported TLS protocol versions and the server's OS/security policies, are not answered bu – Szymon Roziewski Commented Mar 25 at 18:28