Maxscript: dotNet Runtime error: could not create SSL/TLS secure channel


#1

Hello everyone!

I’m having a problem connecting to the internet via maxscript.
I am currently using this code:

fn WebSend theUrl = (
	WebResponse=""
	local spm = dotNetClass "System.Net.ServicePointManager"
	spm.SecurityProtocol = spm.SecurityProtocol.Tls12
	local request = (dotNetClass "System.Net.WebRequest").Create (theUrl)
	request.Accept = @"*/*"
	request.Credentials = (dotnetClass "System.Net.CredentialCache").DefaultCredentials
	request.UseDefaultCredentials = true
	request.Method = "GET"
	request.Timeout = 5000
	request.UserAgent = "3ds Max"
	local response = request.getResponse()
	local responseStream = response.GetResponseStream()
	local encodingClass = dotNetClass "System.Text.Encoding"
	local encoding = encodingClass.GetEncoding "utf-8"
	local readStream = dotNetObject "System.IO.StreamReader" responseStream encoding
	local WebResponse = readStream.ReadToEnd()
	readStream.Close()
	response.Close()
	return WebResponse
)

Mostly this code works, but on some systems it gives this error::

-- Runtime error: dotNet runtime exception: The request was aborted: Could not create SSL/TLS secure channel

After a long search on the Internet, I have not found a solution to this problem. I will be grateful to everyone for any help in solving it.


#2

if you have an access to these systems you could try solutions from SO answers, or you already did that and it doesn’t work?


#3

Hello, Serejah!

Yes, I saw this solution and I understand that I need to add this line before the request = (dotNetClass “System.Net.WebRequest”). Create (theUrl):

ServicePointManager.Expect100Continue = true;

And also these two lines:

ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls12 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11;
ServicePointManager.ServerCertificateValidationCallback = delegate {return true; };

but I don’t know how to correctly list all protocols in maxscript and I don’t know how to write this in maxscript:

ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

If you can tell me how to add this to my code, then I will be very grateful to you.


#4

according to msdn Expect100Continue is set to true by default
to combine enums use dotnet.CombineEnums , or maybe bit.or could work as well
not sure that you need ServerCertificateValidationCallback at all, and if you do then I’d suggest to rewrite this WebSend in c# and call it from maxscript (there’re hundreds of examples how to make a compiled on the fly .dll on this forum )


#5

Hello, Serejan!

Thanks for the tip about dotnet.CombineEnums, this works (bit.or needs no more than 2 values, and I have 4)
As for the ServerCertificateValidationCallback parameter, I’m also not sure what to use it, but your link above indicates that using this parameter exactly solves the problem with the “Could not create SSL / TLS secure channel” error, so I decided to try using it.
I know that it is possible to compile the dll, but I thought that in the case of one parameter, you can get by with the usual use of dotNet in maxscript.
But perhaps in this case it will be enough to list all the protocols to solve this problem.
Thanks for the help anyway.