Click to See Complete Forum and Search --> : WebClient


Sathyaish
February 8th, 2005, 03:44 PM
Okay, now I am using the WebClient class of the .NET framework in Visual Basic .NET 2003 to POST some XML data to a PHP script on some web server. Here's my code:

RELEVANT SNIPPET


Public Function PostMessage(ByVal Data As String, _
ByVal UserName As String, _
ByVal Password As String, _
ByVal Server As String) As Boolean

On Error GoTo Err
Dim bPost As Byte()
Dim StrResponse As String
Dim bResponse As Byte()
Dim Cr As NetworkCredential

Dim myWebClient As New WebClient
Cr = New NetworkCredential(UserName, Password, Server)
myWebClient.Credentials = Cr
myWebClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded")
myWebClient.Headers.Add("Content-Length", Data.Length)
bPost = Encoding.ASCII.GetBytes(Data)
Call WriteToLog("Request : " & Data.ToString)

bResponse = myWebClient.UploadData(Server, "POST", bPost)
Call WriteToLog("Posted. Waiting for response from " & myWebClient.Site.Name.ToString & vbNewLine & myWebClient.ToString)
StrResponse = Encoding.ASCII.GetString(bResponse)
MsgBox(myWebClient.ResponseHeaders.ToString)
Call SaveResponseToFile(StrResponse)
WriteToLog("ShellExecute returned: " & ShellExecute(0, "Open", FILE_RESPONSE, vbNullString, vbNullString, SW_MAXIMIZE))

PostMessage = True
Err:
If Err.Number <> 0 Then
Dim StrError As String
StrError = StrError & "ERROR INFORMATION" & vbNewLine & "Description: " & _
Err.Description & vbNewLine & "Line Number: " & Err.Erl.ToString & vbNewLine & "Source: " & _
Err.Source & vbNewLine & "Expception Message: " & Err.GetException.Message.ToString & vbNewLine & _
"Exception Source: " & Err.GetException.Source.ToString & vbNewLine & "Exception Stack Trace: " & _
Err.GetException.StackTrace.ToString & vbNewLine & "Exception Target Site: " & _
Err.GetException.TargetSite.Name.ToString
MsgBox(Err.Description)
Call WriteToLog(StrError)
Err.Clear()
Exit Function
End If
End Function


I get this exception:

Time: 09/02/05 2:45:24 AM
Event Description:
ERROR INFORMATION
Description: An exception occurred during a WebClient request.
Line Number: 0
Source: System
Expception Message: An exception occurred during a WebClient request.
Exception Source: System
Exception Stack Trace: at System.Net.WebClient.UploadDataInternal(String address, String method, Byte[] data, WebRequest& request)
at System.Net.WebClient.UploadData(String address, String method, Byte[] data)
at Comcate_Outlook_Module.Globals.PostMessage(String Data, String UserName, String Password, String Server)
Exception Target Site: UploadDataInternal



My question(s) are:
(1) What all exceptions must I expect and catch? What exceptions does the WebClient class raise?

(2) Unlike the HTTPWebResponse class, the WebClient class does not spit out a WebResponse/HTTPWebResponse object from which I can get the HTTP Status Code. Instead, it returns a byte array containing the response. It also has a ResponseHeaders collection. How do I retrieve the response code to see if the response was 200 (success)?