r/visualbasic • u/davef139 • 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
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