Solved

The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel

Posted on 2011-09-16
4
1,145 Views
Last Modified: 2012-05-12
Ive written a simple script to pull in a webpage from a https web server on my local network, however it keeps coming up telling me:-

WebException was unhandled:
The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.

I havnt got a signed certificate installed, so if I manually goto the site using internet explorer I get a certificate error, but I just click continue and it works beutifully.

Is there a simple way to just tell my program to ignore the error and just get on with it?


Imports System.Net
Imports System.IO

Module Module1
    Sub Main()
        Dim request As WebRequest = WebRequest.Create("https://192.168.1.7/log/display.php")
        request.Credentials = New System.Net.NetworkCredential("tcross", "qwertyuiop")
        Dim response As WebResponse = request.GetResponse()
        Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
        Dim dataStream As Stream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        Console.WriteLine(responseFromServer)
        reader.Close()
        response.Close()
    End Sub
End Module

Open in new window


Thanks in advance.
0
Comment
Question by:tonelm54
  • 2
4 Comments
 
LVL 18

Expert Comment

by:Dennis Aries
ID: 36549536
Why not create a self-signed certificate to avoid your problem?
See this link for more information.

Otherwise, check this blog-entry
0
 
LVL 11

Expert Comment

by:lenordiste
ID: 36556695
you could also go for something like this:
http://stackoverflow.com/questions/1301127/how-to-ignore-a-certificate-error-with-c-2-0-webclient-without-the-certificate

this solution is similar to one of the links posted by djjaries except it saves you from having to implement an ICertificatePolicy class. The idea is basically the same though: you have to mess up with the ServicePointManager.
0
 

Author Comment

by:tonelm54
ID: 36558582
Good morning,
Unfortantly I dont own the site, so cannot touch it. Im trying to automate the download of a report, which is currently done manually every morning.

I have gone with trying the 'InitiateSSLTrust' and tried to put it in my code:-
Imports System.Net
Imports System.IO
Imports System.Net.Security

Module Module1
    Sub Main()
        Dim request As WebRequest = WebRequest.Create("https://192.168.1.7/log/display.php")
        request.Credentials = New System.Net.NetworkCredential("tcross", "qwertyuiop")
        Dim response As WebResponse = request.GetResponse()
        Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
        Dim dataStream As Stream = response.GetResponseStream()
        Dim reader As New StreamReader(dataStream)
        Dim responseFromServer As String = reader.ReadToEnd()
        Console.WriteLine(responseFromServer)
        reader.Close()
        response.Close()
    End Sub

    Public Sub InitiateSSLTrust()
        Try
            ServicePointManager.ServerCertificateValidationCallback = New RemoteCertificateValidationCallback(Function() True)
        Catch ex As Exception
            'ActivityLog.InsertSyncActivity(ex)
        End Try
    End Sub
End Module

Open in new window


But I still get the same 'The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel'.

Any ideas?
0
 
LVL 11

Accepted Solution

by:
lenordiste earned 500 total points
ID: 36562573
unless I am mistaken you are not even calling your sub InitiateSSLTrust. Make sure you call it at the beginning of your main sub:

Sub Main()
        InitiateSSLTrust()
        Dim request As WebRequest = WebRequest.Create("https://192.168.1.7/log/display.php")
       ....

Open in new window

0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ADO.NET ENTITY DATA MODEL 3 32
Help with LINQ and XML 10 27
VB.Net - Windows UserName index and length error 4 22
Write to file when application closes (by logoff) - VB.NET 1 21
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now