2

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);
    }
 }
}`
Ban_F
  • 23
  • 1
  • 5

1 Answers1

0

Before you get the response you can set the request's HTTP headers via the Headers property of the HttpWebRequest:

'Create initial 
Dim r As HttpWebRequest = HttpWebRequest.Create(txt_Go.Text)

'Set the header(s) here
r.Headers("X-Bans-Header") = "Key"
r.Headers("X-Other-Header") = "Test"

'Create response
'...
Georg Jung
  • 949
  • 10
  • 27
  • Thank you for this. However, when I run this after setting the header, I get the error, "exception unhandled" directing me to the line `Dim response as HttpWebResponse = r.GetResponse` and I haven't got a clue as to why this is. – Ban_F Oct 23 '18 at 11:18
  • Could you please post the more details like the exact error message? Please copy the exception details using the link seen in [this screenshot](https://stackoverflow.com/questions/46814307/view-details-for-exception-in-vs-2017) and paste them here. – Georg Jung Oct 23 '18 at 11:31
  • This seems to be specific for the host you're talking to. It closes the connection. Maybe you pass the key in a wrong way, connect to a wrong port, send the wrong address or something else. Without further information about your specific host and request this is impossible to know. To test the sending of the headers you might try sending custom headers to a service like [this one](http://scooterlabs.com/echo). – Georg Jung Oct 23 '18 at 12:22
  • 1
    @Ban_F APIs usually need a `HTTPS` protcol, which implies a `SSL` handshake. If no `SSL` protocol is specified, `SSL3` and `TLS1.0` are used. This will cause the almost immediate abort of the transaction (usually base on `TLS1.2` these days). Other factors may cause a connection to drop. These are just the most visibly missing settings in your code. – Jimi Oct 24 '18 at 00:46
  • Thank you for the comments. I have added example code from C# that is present on the API's website, hopefully you can help now. – Ban_F Oct 24 '18 at 14:17
  • Please try adding [these three lines](https://stackoverflow.com/a/51977708/1200847) before executing the request - i.e. before the `Try`. This is based on @Jimi's advice to use a newer version of TLS. – Georg Jung Oct 24 '18 at 18:06
  • [Understand HttpWebRequest in keepAlive mode](https://stackoverflow.com/questions/49554203/understand-httpwebrequest-in-keepalive-mode?answertab=active#tab-top). -- [.net 4.61 httpwebrequest on all .tv domains fail...](https://stackoverflow.com/questions/48311006/net-4-61-httpwebrequest-on-all-tv-domains-fail-especially-https-www-tvone-t?answertab=active#tab-top) -- C# related sample, if needed [Which TLS version was negotiated?](https://stackoverflow.com/questions/48589590/which-tls-version-was-negotiated?answertab=active#tab-top) – Jimi Oct 24 '18 at 18:15
  • @GeorgJung The three lines you asked me to add worked! Thank you ever so much! – Ban_F Oct 25 '18 at 17:45