Fog Creek Software
Discussion Board




Welcome! and rules

Joel on Software

WebClient

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)?

Sathyaish Chakravarthy
Tuesday, February 08, 2005

Stop using those crappy VB Err.Description and Err.Number things. Use Try...Catch and you'll find the Exception type that you catch (likely WebException) will have useful properties on it (such as Response, Status, etc.)

"What all exceptions must I expect and catch" - from the docs:

"Remarks
The WebException class is thrown by classes descended from WebRequest and WebResponse that implement pluggable protocols for accessing the Internet.

When WebException is thrown by a descendant of the WebRequest class, the Response property provides the Internet response to the application."
ms-help://MS.MSDNQTR.2004JUL.1033/cpref/html/frlrfsystemnetwebexceptionclasstopic.htm

Duncan Smart
Wednesday, February 09, 2005

Thanks, Duncan. I replaced it with a Try and Catch block. Here's the exception message I get. What help can I get on this?

Time: 16/02/05 2:10:40 AM
Event Description:
An exception occurred during a WebClient request.
UnknownError

It occurs exactly on the UploadData() call. Here's the code (the same basically):


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

        Dim StrResponse As String
        Dim Cr As NetworkCredential
        Dim myWebClient As New WebClient

        Try
            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)
            Dim bPost As Byte() = Encoding.ASCII.GetBytes(Data)
            Dim bResponse As Byte() = myWebClient.UploadData(Server, "POST", bPost)
            StrResponse = Encoding.ASCII.GetString(bResponse)
        Catch ex As WebException
            MsgBox(ex.Message, , ex.Status.ToString)
            Call WriteToLog(ex.Message & vbNewLine & ex.Status.ToString)
        End Try
        Call WriteToLog("Posted. Waiting for response from " & myWebClient.Site.Name.ToString & vbNewLine & myWebClient.ToString)
        MsgBox(myWebClient.ResponseHeaders.ToString)
        Call SaveResponseToFile(StrResponse)
        WriteToLog("ShellExecute returned: " & ShellExecute(0, "Open", FILE_RESPONSE, vbNullString, vbNullString, SW_MAXIMIZE))

        PostMessage = True
    End Function

Sathyaish Chakravarthy
Tuesday, February 15, 2005

*  Recent Topics

*  Fog Creek Home