?
Solved

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

Posted on 2011-09-16
4
Medium Priority
?
1,214 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 2000 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

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

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

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…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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