HttpHelper (VB.NET)
A comprehensive utility class for making HTTP requests and handling responses, with support for various HTTP methods, authentication, and data formats.
Features
- HTTP methods (GET, POST, PUT, DELETE, etc.)
- Request/response handling
- Authentication support
- Cookie management
- Proxy configuration
- Retry policies
- Request timeouts
- Response caching
Installation
Add the HttpHelper.vb
file to your project.
Imports JVR.Helpers
Usage Examples
Basic HTTP Requests
Dim http As New HttpHelper()
' GET request
Dim response As String = Await http.GetAsync("https://api.example.com/data")
' POST request with JSON
Dim data = New With {.name = "John", .age = 30}
Dim response As String = Await http.PostJsonAsync("https://api.example.com/users", data)
' PUT request with form data
Dim formData As New Dictionary(Of String, String) From {{"key", "value"}}
Dim response As String = Await http.PutFormAsync("https://api.example.com/update", formData)
Authentication
Dim http As New HttpHelper()
' Basic authentication
http.SetBasicAuth("username", "password")
' Bearer token
http.SetBearerToken("your-token-here")
' Custom headers
http.AddHeader("API-Key", "your-api-key")
Advanced Features
Dim http As New HttpHelper()
' Configure retry policy
http.ConfigureRetry(maxAttempts:=3, retryDelay:=TimeSpan.FromSeconds(1))
' Set timeout
http.SetTimeout(TimeSpan.FromSeconds(30))
' Download file
Await http.DownloadFileAsync("https://example.com/file.pdf", "local-file.pdf")
API Reference
HTTP Methods
GetAsync(url As String) As Task(Of String)
: Performs GET requestPostAsync(url As String, content As String) As Task(Of String)
: Performs POST requestPutAsync(url As String, content As String) As Task(Of String)
: Performs PUT requestDeleteAsync(url As String) As Task(Of String)
: Performs DELETE requestPatchAsync(url As String, content As String) As Task(Of String)
: Performs PATCH request
JSON Operations
GetJsonAsync(Of T)(url As String) As Task(Of T)
: GET request with JSON responsePostJsonAsync(Of T)(url As String, data As Object) As Task(Of T)
: POST request with JSON dataPutJsonAsync(Of T)(url As String, data As Object) As Task(Of T)
: PUT request with JSON data
Form Data
PostFormAsync(url As String, data As Dictionary(Of String, String)) As Task(Of String)
: POST form dataPutFormAsync(url As String, data As Dictionary(Of String, String)) As Task(Of String)
: PUT form dataUploadFileAsync(url As String, filePath As String) As Task(Of String)
: Upload file
Configuration
SetTimeout(timeout As TimeSpan)
: Sets request timeoutSetBasicAuth(username As String, password As String)
: Sets basic authenticationSetBearerToken(token As String)
: Sets bearer tokenAddHeader(name As String, value As String)
: Adds custom headerConfigureProxy(proxyUrl As String)
: Configures proxy
Response Handling
IsSuccessStatusCode(response As HttpResponseMessage) As Boolean
: Checks response statusGetResponseHeaders(response As HttpResponseMessage) As HttpResponseHeaders
: Gets response headersGetResponseContent(response As HttpResponseMessage) As Task(Of String)
: Gets response content
Dependencies
- .NET 6.0 or higher
- System.Net.Http
- System.Text.Json
- System.Net.Http.Json
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contact
- Website: jvrsoftware.co.za
- Email: jvrsoftware@gmail.com or jan@jvrsoftware.co.za
Version History
- 2.0.1: Added retry policies
- 2.0.0: Added response caching
- 1.0.0: Initial release with basic HTTP operations
Legal Disclaimer
This documentation and associated helper scripts are provided "as is" without warranty of any kind, either express or implied.
- The code examples and helper functions are for illustrative purposes only.
- Users should thoroughly test any implementation in their specific environment.
- The authors are not responsible for any issues or damages arising from the use of these scripts.
- Always follow security best practices and your organization's coding guidelines.
