title: How to get an OAuth 2.0 access token in VB.NET
body: <p>How can I request a token by exchanging basic credentials (client id and secret) (OAuth authentication) in VB.NET?</p>
  <p>I tried the code below without any success. I get the error saying:</p>
  <blockquote>
  <p>The underlying connection was closed: An unexpected error occurred on a send.</p>
  </blockquote>
  <h3>Code</h3>
  <pre><code>Function test()
      Try
          Dim myHttpWebRequest As HttpWebRequest = CType(WebRequest.Create(&quot;https://www.ia.provider.com/access_token&quot;), HttpWebRequest)
          myHttpWebRequest.Method = &quot;POST&quot;
          myHttpWebRequest.ContentType = &quot;application/json&quot;
          myHttpWebRequest.Headers.Add(&quot;Authorization&quot;, &quot;Basic &quot; &amp; System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(&quot;545874a9f5134ed0b54545:333650277fa4d50934c65AAb46Ad123&quot;)))
  
          Dim myHttpWebResponse As HttpWebResponse CType(myHttpWebRequest.GetResponse(), HttpWebResponse)
  
          Dim myreader As New System.IO.StreamReader(myHttpWebResponse.GetResponseStream)
          Dim myText As String
          myText = myreader.ReadToEnd
          Console.WriteLine(myText)
  
      Catch e As ArgumentException
  
          Console.WriteLine(e.Message)
          MsgBox((vbLf &amp; &quot;WebException is thrown. &quot; &amp; vbLf &amp; &quot;Message is:&quot; &amp; e.Message))
  
      Catch e As WebException
          Console.WriteLine(vbLf &amp; &quot;WebException is thrown. &quot; &amp; vbLf &amp; &quot;Message is :&quot; &amp; e.Message)
          If e.Status = WebExceptionStatus.ProtocolError Then
              Console.WriteLine(&quot;Status Code: {0}&quot;, CType(e.Response, HttpWebResponse).StatusCode)
              Console.WriteLine(&quot;Status Description: {0}&quot;, CType(e.Response, HttpWebResponse).StatusDescription)
              Console.WriteLine(&quot;Server : {0}&quot;, CType(e.Response, HttpWebResponse).Server)
          End If
  
      Catch e As Exception
          Console.WriteLine(&quot;Exception is thrown. Message is:&quot; &amp; e.Message)
  
      End Try
  End Function
  </code></pre>