Daily Archives: November 18, 2020

VB.NET || How To Send, Post & Process A REST API Web Request Using VB.NET

The following is a module with functions which demonstrates how to send and receive a RESTful web request using VB.NET.

Contents

1. Overview
2. WebRequest - GET
3. WebRequest - POST
4. WebRequest - PUT
5. WebRequest - PATCH
6. WebRequest - DELETE
7. Utils Namespace
8. More Examples


1. Overview

The following functions use System.Net.HttpWebRequest and System.Net.HttpWebResponse to send and process requests. They can be called synchronously or asynchronously. This page will demonstrate using the asynchronous function calls.

The examples on this page will call a test API, and the resulting calls will return Json results.

The Json objects we are sending to the API are hard coded in the examples below. In a real world application, the objects would be serialized first before they are sent over the network, and then deserialized once a response is received. For simplicity, those operations are hard coded.

For examples of how to serialize and deserialize objects using Json and XML, see below:

Note: Don’t forget to include the ‘Utils Namespace‘ before running the examples!


2. WebRequest – GET

The example below demonstrates the use of ‘Utils.WebRequest.Get‘ to execute a GET request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


3. WebRequest – POST

The example below demonstrates the use of ‘Utils.WebRequest.Post‘ to execute a POST request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


4. WebRequest – PUT

The example below demonstrates the use of ‘Utils.WebRequest.Put‘ to execute a PUT request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


5. WebRequest – PATCH

The example below demonstrates the use of ‘Utils.WebRequest.Patch‘ to execute a PATCH request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


6. WebRequest – DELETE

The example below demonstrates the use of ‘Utils.WebRequest.Delete‘ to execute a DELETE request on the given url.

The optional function parameter allows you to specify System.Net.HttpWebRequest options, like the UserAgent, Headers etc.


7. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


8. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.

VB.NET || How To Check If A String Is A Valid HTTP URL Using VB.NET

The following is a module with functions which demonstrates how to check whether a string is a valid HTTP URL using VB.NET.

Note: The following function only checks for valid url formatting. It does not determine if the address behind the url is valid for navigation.


1. Is Valid URL

The example below demonstrates the use of ‘Utils.Http.IsValidURL‘ to check whether a string is a valid HTTP URL.


2. Utils Namespace

The following is the Utils Namespace. Include this in your project to start using!


3. More Examples

Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!

QUICK NOTES:
The highlighted lines are sections of interest to look out for.

The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.