Using System.Web.WebClient

Posted by jamesmc on 15-Apr-2016 12:00

Hi,

I am successfully using the WebClient class in my code to send data to a server using the UploadValues method.  When this works I receive a JSON object back containing, among other things, a status code of 1.  If I send some invalid data to the server I should still receive a JSON object back but this time the status code should be 0 to indicate there was a problem.  When the server issues a status of 0 it also raises a HTTP 4xx error which the WebClient throws as a System.Net.WebException.  Is there a way to ignore the WebException because when my CATCH block catches it I dont actually receive my JSON object.

I am using 10.2B08.

Thanks.

My code looks like the following:

ASSIGN
    lvWebClient = NEW System.Net.WebClient().

ASSIGN
    lvByteResult = lvWebClient:UploadValues("serveraddress", lvParameters).

ASSIGN
    lvEncoder = NEW System.Text.ASCIIEncoding().

ASSIGN
    lvJsonString = lvEncoder:GetString(lvByteResult).

ASSIGN
    lvReturnValue = TRUE.

CATCH e AS System.Net.WebException:

    ASSIGN
        THIS-OBJECT:ErrorMessage = e:getMessage(1).

END CATCH.

Posted by Peter Judge on 15-Apr-2016 12:14

If you look at msdn.microsoft.com/.../system.net.webexception(v=vs.110).aspx you should be able to get the returned JSON via the WebException's Response property (which is a WebResponse and which should have the body attached to it somehow)
 

All Replies

Posted by Peter Judge on 15-Apr-2016 12:14

If you look at msdn.microsoft.com/.../system.net.webexception(v=vs.110).aspx you should be able to get the returned JSON via the WebException's Response property (which is a WebResponse and which should have the body attached to it somehow)
 

Posted by jamesmc on 22-Apr-2016 04:24

Thanks Peter.  I have sorted this now and for anyone who may need it in the future here is what I did:

CATCH we AS System.Net.WebException:

    DEFINE VARIABLE lvReader AS CLASS System.IO.StreamReader NO-UNDO.
                
    ASSIGN
        lvReader = NEW System.IO.StreamReader(we:Response:GetResponseStream()).

    ASSIGN
        THIS-OBJECT:ErrorMessage = we:getMessage(1).

    lopResponse:ParseXmlResponse(INPUT lvReader:ReadToEnd()).

END CATCH.

This thread is closed