dotNetClass "System.Net.WebRequest" stopped working!


#1

Hi, all!

The following code worked fine always to connect to my website via HTTP, but since the connection to the site changed to HTTPS (a security certificate was installed), it stopped working.
Please, tell me how to solve this problem and how to make changes to the code so that it works with web sites using the HTTPS protocol.
Or maybe the reason is different?

Thank you in advance for any help!


fn webRequest = (
try (
local request = (dotNetClass "System.Net.WebRequest").Create ("https://www.mywebsite.com");
request.Method = "GET"
request.Timeout = 1000
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 webanswer = readStream.ReadToEnd()
readStream.Close()
response.Close()
return webanswer
)
catch(print (getCurrentException()))
)
webRequest()

Result:


-- Runtime error: dotNet runtime exception: Authentication failed because the remote party has closed the transport stream.


#2

I had an issue with a webrequest a while back and for me changing the security protocol did the trick. I did it in c#, but for maxscript it might be something like this
[```
spm = dotnetclass “System.Net.ServicePointManager”
spm.SecurityProtocol = spm.SecurityProtocol.Tls12


#3

Thank you very much for your response, grabjacket!
I also think that there is a problem with the TLS security protocol, but I do not know how it should look in mascript.
How to add your example to my code and how to associate the spm parameter in your example with the request or response parameters in my code?


#4

Hey dZorg, I actually implemented this in c#, so I’m unsure about the syntax in maxscript exactly, but I’ve used this post as a reference: https://stackoverflow.com/a/37869237
You can just add this statement before making the request. There’s no need to associate it with your code.


#5

Many thanks, grabjacket!
This method works great!:thumbsup:


#6

Hello everyone!

I bring up the topic again, as the problem still remains.
I am currently using this code to connect to internet via maxscript:

    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 without errors, but this some users it throws an error like this:

-- 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.