r/visualbasic • • Aug 25 '20

VB.NET Web API POST

I recently fired up an old project. I did some searching and it appears my old methods are quite outdated. Can anyone point me in the direction on the updated?

Basically trying to send POST requests to an API and capture the response. I was using something basic like the below. From looking the webrequest/response has been replaced?

        Dim reqstring As String = ("http://production.shippingapis.com/ShippingAPI.dll?API=TrackV2&XML=<TrackRequest USERID='user'><TrackID ID='trkno'></TrackID></TrackRequest>")
        Dim TrackRequest As WebRequest = WebRequest.Create(reqstring)
        Dim responce As WebResponse = TrackRequest.GetResponse()
        Dim datastream As Stream = responce.GetResponseStream()
        Dim reader As New StreamReader(datastream)
        Dim responcefromserver As String = reader.ReadToEnd()
        Dim xmlout As XDocument = XDocument.Parse(responcefromserver)
7 Upvotes

10 comments sorted by

1

u/andrewsmd87 Web Specialist Aug 25 '20

I mean, it's VB, which is outdated in itself, so if it's working, I'd probably leave it alone.

However, if you really want to scrutinize this, then you could be using the newer httpwebrequest classes, as well as your stream reader needing to be in a using clause.

But, like I said, if it's working, I wouldn't go to the hassle

1

u/davef139 Aug 25 '20

I need to update and make some changes to some other API's for JSON and such, so I plan to overhaul for better uniformity.

1

u/andrewsmd87 Web Specialist Aug 25 '20

Are you doing a complete rebuild? Or just minor refactoring?

1

u/davef139 Aug 25 '20

Prob a larger overhaul as I plan to add quite a bit more API requests and such this covered a single, while I would like to access about 10. I will poke around the httpwebrequest classes.

1

u/andrewsmd87 Web Specialist Aug 25 '20

Not sure if it's feasible but if you're doing a large overhaul, I'd move to c#. The language is pretty translatable from VB and VB will at some point be not supported.

It would also allow you to move to .net core. Freeing up hosting restrictions to windows boxes

1

u/davef139 Aug 25 '20

Everything i have is winforms so the frontend is done. I just gotta work backend pulling my data in, my desire to change up everything isn't there either.

1

u/andrewsmd87 Web Specialist Aug 25 '20

Yea I get that, I just didn't know what "larger overhaul" encompassed.

The xml that you parse, is that in a class and objects, or are you using the old xml.childnode type stuff? That might be another area to look into. Xml/Json serialization is amazing

1

u/Medyu Aug 25 '20

I do it like this:

        address = New Uri(Url)
        request = DirectCast(WebRequest.Create(address), HttpWebRequest)

        request.Method = "POST"
        request.ContentType = "text/xml;charset=UTF-8"
        request.Headers.Add("SOAPAction", SoapAction)
        byteData = UTF8Encoding.UTF8.GetBytes(DataSend)
        request.ContentLength = byteData.Length

        Try
            postStream = request.GetRequestStream()
            postStream.Write(byteData, 0, byteData.Length)
            postStream.Close()

            response = DirectCast(request.GetResponse(), HttpWebResponse)
            reader = New StreamReader(response.GetResponseStream())
            Dim xmlread As String = reader.ReadToEnd()
            response.Close()
            return xmlread

        Catch ex As WebException

2

u/davef139 Aug 27 '20

Great this pretty much worked! Got my POST working and returns an xml, now just to be able to create them better.

1

u/davef139 Sep 01 '20

One more thing. I got the request/reply down, but I am having issues with the SOAP part and putting my return as an XDOC, would like to xpath into specific values. my basic response..

I am just trying to hit some values within, such as the SUCCESS and then various ones that i chopped off.

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header/><SOAP-ENV:Body><TrackReply xmlns="http://fedex.com/ws/track/v18"><HighestSeverity>SUCCESS</HighestSeverity><Notifications><Severity>SUCCESS</Severity><Source>trck</Source><Code>0</Code><Message>Request was successfully processed.</Message><LocalizedMessage>Request was successfully processed.</LocalizedMessage></Notifications><TransactionDetail><CustomerTransactionId>Track By Number_v18</CustomerTransactionId><Localization><LanguageCode>EN</LanguageCode><LocaleCode>US</LocaleCode></Localization></TransactionDetail><Version><ServiceId>trck</ServiceId><Major>18</Major><Intermediate>0</Intermediate><Minor>0</Minor></Version><CompletedTrackDetails><HighestSeverity>SUCCESS</HighestSeverity><Notifications><Severity>SUCCESS</Severity><Source>trck</Source><Code>0</Code><Message>Request was successfully processed.</Message><LocalizedMessage>Request was successfully processed.</LocalizedMessage></Notifications><DuplicateWaybill>false</DuplicateWaybill><MoreData>false</MoreData><TrackDetailsCount>0</TrackDetailsCount><TrackDetails><Notification><Severity>SUCCESS</Severity><Source>trck</Source><Code>0</Code><Message>Request was successfully processed.</Message></TrackReply></SOAP-ENV:Body></SOAP-ENV:Envelope>