Share via


Unable to connect to remote server (security restrictions)

Question

Monday, October 23, 2017 11:03 AM

I have a product designed which works perfectly under most conditions, but at a certain location where I access the companies' network, I'm guessing their security prevents my software from operating as it should. 

I'm getting the exception "unable to connect to remote server" at the line of code:

                    response = DirectCast(clsRequest.GetResponse(), System.Net.FtpWebResponse)

I need to learn why this doesn't work at this location but works pretty much everywhere else? I'm sure it has something to do with the network security. The goal in this part of the code is to download a simple encrypted text file.

The entire section of code:

                    clsRequest = DirectCast(System.Net.WebRequest.Create("ftp://" & CredentialsHostingUsername & ":" & CredentialsHostingPassword & "HostingLocation & "/File.DAT"), System.Net.FtpWebRequest)
                    clsRequest.Credentials = New System.Net.NetworkCredential(CredentialsHostingUsername, CredentialsHostingPassword)

                    clsRequest.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
                    clsRequest.EnableSsl = True

                    response = DirectCast(clsRequest.GetResponse(), System.Net.FtpWebResponse)
                    clsStream = response.GetResponseStream()

                    Dim outFile As New System.IO.FileStream(SaveLocation & "/File.DAT", IO.FileMode.Create)
                    Dim data(512) As Byte
                    Dim bytesRead As Integer

                    Do
                        bytesRead = clsStream.Read(data, 0, data.Length)
                        If bytesRead >= 0 Then
                            outFile.Write(data, 0, bytesRead)
                        End If
                    Loop While bytesRead > 0

Programming is mostly just a hobby for me :)

All replies (16)

Monday, October 23, 2017 1:22 PM | 1 vote

If you were having some kind of security issue with resources on the Windows O/S platform, you would be getting an "Access is denied" to the resource exception being thrown.

"unable to connect to remote server" is telling that the program cannot connect remotely for whatever reason that has nothing to do with network security in the sense that you are looking at it. It could be as simple as a firewall is blocking access, as an example.   


Monday, October 23, 2017 2:58 PM

Thank you for the reply. I'm not at that location every day and am not there now, so I can't do any further testing at this time. The program consistently doesn't work there and consistently works everywhere else, so I'm stumped. I may be overlooking something simple, so will keep looking into this. 

Programming is mostly just a hobby for me :)


Tuesday, October 24, 2017 6:47 AM | 1 vote

Hi programmer4life,

According to your description and my search, Is there a situation with your firewall might be blocking the ports? Please take a look services on a local machine.

Best Regards,

Cherry

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Wednesday, October 25, 2017 10:37 AM

I just turned off my firewall completely and am getting the same error. Is there something else I can look at to determine why it does not connect? 

Programming is mostly just a hobby for me :)


Wednesday, October 25, 2017 3:14 PM

I just turned off my firewall completely and am getting the same error. Is there something else I can look at to determine why it does not connect? 

Programming is mostly just a hobby for me :)

The problem could be at the gateway firewall.


Wednesday, October 25, 2017 4:55 PM

Did you try using Try...Catch for WebException and then checking the Status for more info?

Paul ~~~~ Microsoft MVP (Visual Basic)


Wednesday, October 25, 2017 5:42 PM

Yes, it says "Unable to connect to remote server" What specific part of the exception to you want to see the info for? I'm not at that location now but will be back on Friday. 

Programming is mostly just a hobby for me :)


Wednesday, October 25, 2017 6:24 PM

Yes, it says "Unable to connect to remote server" What specific part of the exception to you want to see the info for? I'm not at that location now but will be back on Friday. 

Programming is mostly just a hobby for me :)

Are you referring to the standard Exception message or the status properties of WebException. I don't see the Try...Catch block in your code so I can't tell where that exception message is coming from.

Paul ~~~~ Microsoft MVP (Visual Basic)


Wednesday, October 25, 2017 6:51 PM

That's a small part of the code in a large try block and the error is coming from the line:

response = DirectCast(clsRequest.GetResponse(), System.Net.FtpWebResponse)

Would you like me to post something else from the exception? 

Programming is mostly just a hobby for me :)


Wednesday, October 25, 2017 7:06 PM

That's a small part of the code in a large try block and the error is coming from the line:

response = DirectCast(clsRequest.GetResponse(), System.Net.FtpWebResponse)

Would you like me to post something else from the exception? 

Programming is mostly just a hobby for me :)

Did you look at the link in my post for WebException? That should contain an example of the properties I am referring to.

Paul ~~~~ Microsoft MVP (Visual Basic)


Wednesday, October 25, 2017 7:28 PM

I will post that Friday when I return there. Just to be sure it isn't the computer, the same project runs as it should from home, so I can't see how it isn't related to the security of the clinic's network. 

Programming is mostly just a hobby for me :)


Wednesday, October 25, 2017 7:33 PM

p4l,

Try to connect to it a different way like, for instance, using a WebClient.

Whether you upload a test file or download one doesn't matter - the test is the connection itself. See if that reports an exception and if so, just what.

"A problem well stated is a problem half solved.” - Charles F. Kettering


Friday, October 27, 2017 11:02 AM

Well, I can get online at the clinic and download zip files but not exe files from my site. The WebClient code doesn't do anything. Do I need to add a button and tell it to do something?

Programming is mostly just a hobby for me :)


Friday, October 27, 2017 11:12 AM

Well, I can get online at the clinic and download zip files but not exe files from my site. The WebClient code doesn't do anything. Do I need to add a button and tell it to do something?

Programming is mostly just a hobby for me :)

I assume you're talking to me? We don't all see this forum the same way and I can't tell.

*****

The WebClient has a lot of methods:

https://msdn.microsoft.com/en-us/library/system.net.webclient_methods(v=vs.110).aspx

You might want to look at some of the async methods because with those, you can then subscribe to various events.

Do I understand though that with the WebClient you were able to connect where you couldn't before? If so, look carefully at how yours is set up (credentials come to mind) and see if you can spot an anomaly somewhere.

"A problem well stated is a problem half solved.” - Charles F. Kettering


Friday, October 27, 2017 1:14 PM

Well, I can get online at the clinic and download zip files but not exe files from my site. The WebClient code doesn't do anything. Do I need to add a button and tell it to do something?

Programming is mostly just a hobby for me :)

Vista and up O/S(s) are not going to allow the downloading of a file with an exe extension from a foreign site. If you are getting a way with this elsewhere, I don't know how you are doing it.  


Monday, October 30, 2017 10:38 AM

Vista and up O/S(s) are not going to allow the downloading of a file with an exe extension from a foreign site. If you are getting a way with this elsewhere, I don't know how you are doing it.  

Downloading .exe files are easy, just as long as the network allows it. In cases where the network doesn't allow it, I simply download a .zip and extract the .exe. The line of code I use to download any file on a Win 10 system is:

            My.Computer.Network.DownloadFile("http://www.MySite.com/FILES/MyFile.exe", "C:/SomeFolder/MyFile.exe")

Why would Microsoft be concerned with people having the ability to design software that downloads an .exe when it is just as easy to simply download a .zip and extract the same .exe and run it? In my opinion, it is pointless for network administrators to restrict downloading .exe files for this same reason. 

I still haven't gotten to the bottom of why I cannot access my server files through the clinic network, but have no issue running the same code from my main system at home. I guess it isn't critical I fix this issue; I'm mostly just curious. 

Programming is mostly just a hobby for me :)