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