That is quite a big ask for the forums. Have you looked at any of the existing documentation and samples on how to call a REST API from .NET? There are plenty of examples. APIs vary wildly so some of this requires that you understand how the API you're calling works.
https://www.programmingnotes.org/6229/vb-net-how-to-send-post-process-a-rest-api-web-request-using-vb-net/
https://social.msdn.microsoft.com/Forums/en-US/bd62538b-1adc-4064-86ec-c093cd754e08/vbnet-rest-api-call-with-basic-authorization?forum=vbgeneral
https://stackoverflow.com/questions/56173249/calling-rest-api-from-visual-basic
Writing API calls by hand is pretty easy but requires experience. A faster approach to get started, if you're using Visual Studio 2019 or newer, is to use Connected Services. This assumes that the API you're calling supports OpenAPI (all public ones do). If this is true then right click your project and select Add -> Connected Service
. In the UI select the option to add a service reference to OpenAPI. Set the URL to the APIs OpenAPI spec (get this URL from the provider) and then click Finish to have it auto-generate all the code for you. You can then calling the API directly using standard .NET types.
If you want to do it manually then it is recommended you create a wrapper class around the client. HttpClient is the core type needed for this to work. Here's an example of what it might look like given just 1 request in your sample.
Imports System
Imports System.Net.Http
Imports System.Net.Http.Json
Imports System.Threading
Class AbcClient
Sub New(client As HttpClient)
_client = client
End Sub
Async Function XyxPushAsync(name As String, title As String, phone As String, file As Byte()) As Task
Dim body = New With {.title = title, .name_applicant = name, .mobile = phone, .upload_file = file}
Using response As HttpResponseMessage = Await _client.PostAsJsonAsync("xyx_push_api.php", body)
response.EnsureSuccessStatusCode()
End Using
End Function
Private ReadOnly _client As HttpClient
End Class
Module Program
Sub Main(args As String())
Dim client = New AbcClient(_httpClient)
client.XyxPushAsync("Bob", "Director", "5551234567", Array.Empty(Of Byte)).Wait()
End Sub
Private ReadOnly _httpClient As HttpClient = New HttpClient() With {
.BaseAddress = New Uri("http://test.xyz.com/abc_webservice/")
}
End Module
Some notes on this code.
-
HttpClient
should only be created once per application per base URL. - URL passed to
HttpClient
must end with slash. - The sample wasn't clear what a "file" was so I assume a byte array. This probably isn't correct so you'll need to adjust.
- If an API returns a response then you'll have to convert the response back to an object you care about using a JSON deserializer.
- APIs will fail periodically so all this should be done using exception handling and the app needs to handle failures.
- The
AbcClient
class can be expanded over time to include additional functions for each API endpoint you intend to call. - The actual client call contains just the part of the URL after the base address that the client already defined.