M
Mike
Does anyone see any problems with this VB .NET code? A customer is using it
and is getting an exception at the line:
myResponse = myRequest.GetResponse()
Private Sub btnTest_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnTest.Click
Dim myRequest As System.Net.WebRequest
Dim myResponse As System.Net.WebResponse
Dim myProxy As New System.Net.WebProxy
Cursor.Current = Cursors.WaitCursor
myRequest = System.Net.WebRequest.Create("http://www.ourserver.com")
myRequest.Timeout = 30000
' Obtain the Proxy Prperty of the Default browser.
myProxy = CType(myRequest.Proxy, System.Net.WebProxy)
If Not myProxy.Address Is Nothing Then
MsgBox("The default Proxy address is: " & myProxy.Address.AbsoluteUri)
End If
Dim newUri As New Uri(txtServer.Text)
' Associate the new Uri object to the myProxy object.
myProxy.Address = newUri
' Create a NetworkCredential object and assign to the Credentials property
of the Proxy object.
myProxy.Credentials = New System.Net.NetworkCredential(txtUser.Text,
txtPassword.Text)
myRequest.Proxy = myProxy
Try
myResponse = myRequest.GetResponse()
MsgBox("Connection worked!")
Catch ex As System.Net.WebException
MsgBox("WebException occurred connecting to server: " & ex.Message)
MsgBox("Exception status = " & ex.Status.ToString)
If Not ex.InnerException Is Nothing Then
MsgBox("InnerException.Message = " & ex.InnerException.Message)
End If
If Not ex.Response Is Nothing Then
MsgBox("Status Code: " & CType(ex.Response,
System.Net.HttpWebResponse).StatusCode)
MsgBox("Status Description: " & ("Status Description: " & CType(ex.Response,
System.Net.HttpWebResponse).StatusDescription))
End If
Catch ex As System.Exception
MsgBox("Unable to connect to server: " & ex.Message)
Finally
Cursor.Current = Cursors.Default
End Try
End Sub
and is getting an exception at the line:
myResponse = myRequest.GetResponse()
Private Sub btnTest_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnTest.Click
Dim myRequest As System.Net.WebRequest
Dim myResponse As System.Net.WebResponse
Dim myProxy As New System.Net.WebProxy
Cursor.Current = Cursors.WaitCursor
myRequest = System.Net.WebRequest.Create("http://www.ourserver.com")
myRequest.Timeout = 30000
' Obtain the Proxy Prperty of the Default browser.
myProxy = CType(myRequest.Proxy, System.Net.WebProxy)
If Not myProxy.Address Is Nothing Then
MsgBox("The default Proxy address is: " & myProxy.Address.AbsoluteUri)
End If
Dim newUri As New Uri(txtServer.Text)
' Associate the new Uri object to the myProxy object.
myProxy.Address = newUri
' Create a NetworkCredential object and assign to the Credentials property
of the Proxy object.
myProxy.Credentials = New System.Net.NetworkCredential(txtUser.Text,
txtPassword.Text)
myRequest.Proxy = myProxy
Try
myResponse = myRequest.GetResponse()
MsgBox("Connection worked!")
Catch ex As System.Net.WebException
MsgBox("WebException occurred connecting to server: " & ex.Message)
MsgBox("Exception status = " & ex.Status.ToString)
If Not ex.InnerException Is Nothing Then
MsgBox("InnerException.Message = " & ex.InnerException.Message)
End If
If Not ex.Response Is Nothing Then
MsgBox("Status Code: " & CType(ex.Response,
System.Net.HttpWebResponse).StatusCode)
MsgBox("Status Description: " & ("Status Description: " & CType(ex.Response,
System.Net.HttpWebResponse).StatusDescription))
End If
Catch ex As System.Exception
MsgBox("Unable to connect to server: " & ex.Message)
Finally
Cursor.Current = Cursors.Default
End Try
End Sub