Solved

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

Posted on 2011-09-16
4
1,131 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

12 Experts available now in Live!

Get 1:1 Help Now