I'm currently trying to create a simple HTTP web request using the following code:
Imports System.Net
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
'Create initial
Dim r As HttpWebRequest = HttpWebRequest.Create(txt_Go.Text)
'Create response
Dim response As HttpWebResponse = r.GetResponse
Dim responsestream As System.IO.Stream = response.GetResponseStream
'Create a new stream reader
Dim streamreader As New System.IO.StreamReader(responsestream)
Dim data As String = streamreader.ReadToEnd
streamreader.Close()
'display data
txt_response.Text = data
Catch ex As Exception
MsgBox("Improper Input")
End Try
End Sub
End Class
Where the Windows Form contains a text box for the user to enter the specific URL for the API, then the API returns results following the user's query. My issue here is I need to set a request header, that contains a key, I'm not sure on how to do this.
Here is an example of the code I'm trying to achieve in C#, I need to convert this to be able to use in VB.
using System;
using System.Net.Http.Headers;
using System.Text;
using System.Net.Http;
using System.Web;
namespace CSHttpClientSample
{
static class Program
{
static void Main()
{
MakeRequest();
Console.WriteLine("Hit ENTER to exit...");
Console.ReadLine();
}
static async void MakeRequest()
{
var client = new HttpClient();
var queryString = HttpUtility.ParseQueryString(string.Empty);
// Request headers
client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "{subscription key}");
var uri = "https://dev.tescolabs.com/grocery/products/?query={query}&offset={offset}&limit={limit}&" + queryString;
var response = await client.GetAsync(uri);
}
}
}`