How Do I Test HTTP / HTTPS / PORT 993 - IMAPs / is Alive TESTs using .NET / VB / C#

I have little monitoring app (Windows Forms - desktop) that pings our mail server
every now and then to acertain that it is alive, that part works ok.

What I now want to do is to test the various services & ports below.
i.e. HTTP / HTTPS / PORT 993 - IMAPs/ is Alive using .NET

These will basically be:

HTTP monitor (HTTP)
HTTP monitor (HTTP)
HTTPS monitor emailsubdomain.isp.yourcompany.comm (HTTPS)
PING monitor (PING)
PORT - IMAPs monitor on IMAP Port 993  

A test sample URL will be like below:

The piece of code I was using for the HTTP is below, but this does not work

I want a function or group of functions that will return TRUE/FALSE for the various
tests above.
Private Sub OnPostInfo()
        Dim strId As String = ""
        Dim strName As String = "domain\firstname.lastname"
            Dim encoding As New ASCIIEncoding()
            Dim postData As String = "userid=" & strId
            postData += ("&username=" & strName)
            Dim data As Byte() = encoding.GetBytes(postData)
            'Prepare web request...
            Dim myRequest As HttpWebRequest = DirectCast(WebRequest.Create(""), HttpWebRequest)
            myRequest.Method = "POST"
            myRequest.ContentType = "application/x-www-form-urlencoded"
            myRequest.ContentLength = data.Length
            Dim newStream As Stream = myRequest.GetRequestStream()
            ' Send the data.
            newStream.Write(data, 0, data.Length)
        Catch ex As Exception
	End Try
End Sub

Open in new window

Who is Participating?
netimpactConnect With a Mentor Author Commented:
Ok, I found another solution that worked using Webclient and User Agent.
You might find the WebRequest class easier to work with:
netimpactAuthor Commented:

Thanks for you reply.

I am using the attached code snippet from MS$ example.

When I use this URL I get an error message as
below. But when I use this URL it seems to be fine.

Any ideas why and how to fix this? Note that the first one is actually an HTTPs.


{"The remote server returned an error: (400) Bad Request."}
"   at System.Net.HttpWebRequest.GetResponse()    
at WebRequestTest.RequestResponse.RequestingaResponse() in C:\download\WebRequestTest\WebRequestTest\RequestIT.vb:line 16"

 Public Shared Sub RequestingaResponse()
            ' Create a request for the URL.         
            Dim request As WebRequest = WebRequest.Create("")
            'Dim request As WebRequest = WebRequest.Create("")
            ' If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials
            ' Get the response.
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
            ' Display the status.
            ' Get the stream containing content returned by the server.
            Dim dataStream As Stream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content. 
            ' Cleanup the streams and the response.
        Catch ex As Exception
        End Try
    End Sub

Open in new window

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Are you able to show me the actual https address you are hitting?
netimpactAuthor Commented:
Its our OWA address. Is there a way I can send this to you privately or hide it from general view?
netimpactAuthor Commented:
How do I add a User Agent to that code? That might work.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.