Solved

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

Posted on 2011-09-16
4
1,163 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

756 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