I've used the WebClient class on a few projects but I wanted to know if
anyone could point to the good resource for Best Practices with this object.
The two things I haven't seen in sample code are:
1. How to retrieve connection error messages, such as a DNS resolution error
or connection dropped errors.
2. Should I use the Dispose or Finalize methods to destroy the object when I
done? I assume that the object uses unmanaged code at some point to connect
to another server. So do I need to use the Finalize method release those
other objects?
Here is a function from one of my projects:
Private Function getRemoteDat() As Boolean
Dim CCPostURL As String =
ConfigurationSettings.AppSettings("https://www.remoteData.com/process.asp")
Dim Status As Boolean = False ' Assumes failure until set to True
Dim ResponseResults As String
Dim responseString As String
Dim myWebClient As New System.Net.WebClient
Dim responseArray As Byte()
Dim myNameValueCollection As New
System.Collections.Specialized.NameValueCollection
Dim ResultMsg As String
Dim splitResponse() As String
myNameValueCollection.Add("first_name", txtBillFName.Text)
myNameValueCollection.Add("last_name", txtBillLName.Text)
myNameValueCollection.Add("address", txtBillAdd1.Text)
Try
responseArray = myWebClient.UploadValues(CCPostURL, "POST",
myNameValueCollection) 'Sends request to Gateway
responseString = System.Text.Encoding.ASCII.GetString(responseArray )
'Converts bit array to string
splitResponse = Split(responseString, System.Environment.NewLine)
'Splits response into string array
ResultMsg = splitResponse(1).Remove(0, 19) 'Gets Result Message
Catch
Status = False 'Connection failed or response was corrupted
End Try
If ResultMsg = "APPROVED" Then
_GatewayApprovalString = splitResponse(2) & "|" & splitResponse(3)
'Set string if successful
Status = True
Else
Status = False
End If
Return Status
End Function