Solved

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

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sending receiving text messages in vb.net 15 39
MVC DDL Json Not Binding to Model 2 13
Syntax Issue with SSIS module 26 59
bound data table problem 2 11
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

825 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