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 =
ConfigurationSe ttings.AppSetti ngs("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.WebC lient
Dim responseArray As Byte()
Dim myNameValueColl ection As New
System.Collecti ons.Specialized .NameValueColle ction
Dim ResultMsg As String
Dim splitResponse() As String
myNameValueColl ection.Add("fir st_name", txtBillFName.Te xt)
myNameValueColl ection.Add("las t_name", txtBillLName.Te xt)
myNameValueColl ection.Add("add ress", txtBillAdd1.Tex t)
Try
responseArray = myWebClient.Upl oadValues(CCPos tURL, "POST",
myNameValueColl ection) 'Sends request to Gateway
responseString = System.Text.Enc oding.ASCII.Get String(response Array)
'Converts bit array to string
splitResponse = Split(responseS tring, System.Environm ent.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
_GatewayApprova lString = splitResponse(2 ) & "|" & splitResponse(3 )
'Set string if successful
Status = True
Else
Status = False
End If
Return Status
End Function