System.Net.WebClient Best Practices

G

Guest

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
 
S

Steven Cheng[MSFT]

Hi jmh,

Welcome to ASP.NET newsgroup.
Regarding on the "System.Net.WebClient Best Practices" you mentioned, here
are some of my suggestions:

The WebClient class in system.net namespace is a well-encapsulated class
which just simulate the IE browser's behavior. When we use it to post data
or retrieve response from remote url, it will help do the underlying
connection and transfering tasks for us(also some additinal works such as
cookie management). In fact, the WebClient class is using the
HttpWebRequest class internally when dealing with http... resource. So if
we need more detailed and
underlying control when doing such work, we can consider directly use the
HttpWebRequest class instead. This class can help us manually write binary
data into the http request's stream and customize the http Message's header
Also, we can get the http status code after we make request and retieve
response from the remote resource. For detailed reference on
HttpWebRequest , you can lookup the MSDN document on it(or the accessing
internet section in "programming with .net section).

In addition, since httpWebrequest have more underlying controls, it also
require us to do more works to properly handle the resource. for example,
we need to manually close the response stream after read it so as not to
occupy the connection. Here are some tech articles and former newsgroup
threads discussing on such problems, such as use httpwebrequest to post
data, upload file or upload files together with form datas:


#Send data from non browser client (using HttpWebRequest) that can be used
by access web page controls.
http://weblogs.asp.net/ngur/archive/2004/05/11/129951.aspx

#how to upload file via c# code
http://groups-beta.google.com/group/microsoft.public.dotnet.languages.csharp
/browse_thread/thread/eea92e16a26fbdc0/474b6a3fcd63dd93?q=C%23+upload+file+s
teven+cheng&rnum=1&hl=en#474b6a3fcd63dd93

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top