none
Accessing an API from a Windows App RRS feed

  • Question

  • Hi, I am writing a windows application in VB on VS2017. I have a need to access some information on the web. The supplier has given the following code. But when I plug it into my application I get error messages that HTTP etc. is not defined. What do I need to do?

    Dim uri As String
    Dim SemiColonSepVins As String
    Dim tmp As System.Net.Http.HttpResponseMessage
    Dim result As System.Threading.Tasks.Task(Of String)
    Try
    	uri = "https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/"
    	SemiColonSepVins = "5XYKT3A12CG000000;3D7KU28C54G000000;"
    	Dim client = New HttpClient()
    	client.BaseAddress = New Uri(uri)
    	Dim nameValues As New Dictionary(Of String, String)
    	nameValues.Add("data", SemiColonSepVins)
    	nameValues.Add("format", "json")
    	Dim name = New FormUrlEncodedContent(nameValues)
    	client.DefaultRequestHeaders.Accept.Add(New System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"))
    	Dim token = New System.Threading.CancellationToken()
    	tmp = client.PostAsync(client.BaseAddress, name, token).Result
    	result = tmp.Content.ReadAsStringAsync()
    Catch ex As Exception
    	' error handling
    End Try
    

    Friday, June 16, 2017 12:58 PM

All replies

  • I think you need to get a book about VB and VS.


    Sam Hobbs
    SimpleSamples.Info

    Friday, June 16, 2017 3:58 PM
  • Thanks for the help. AH
    Friday, June 16, 2017 4:29 PM
  • Hi James,

    Welcome to the MSDN forum.

    Refer to your description, your issue is about VB development. Since our forum is to discuss the VS IDE, I will help you move this thread to the appropriate forum: Visual Studio Languages  >  Visual Basic to seek for a more professional support, thank you for your understanding.

    Best regards,

    Sara


    MSDN Community Support<br/> Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact <a href="mailto:MSDNFSF@microsoft.com">MSDNFSF@microsoft.com</a>.

    Monday, June 19, 2017 2:19 AM
  • The supplier has given the following code.

    Well that's who you need to contact about the issues you are having.

    • Proposed as answer by Cor Ligthert Tuesday, June 20, 2017 8:57 AM
    Monday, June 19, 2017 2:26 AM
  • But when I plug it into my application I get error messages that HTTP etc. is not defined. What do I need to do?

    This is the information that you need to resolve the issue.  The error messages will tell you what the problems are and what you need to do to fix them.  If you need assistance with implementing those fixes, post the exact text of the error messages.

    Monday, June 19, 2017 3:00 AM
  • Hi, I am writing a windows application in VB on VS2017. I have a need to access some information on the web. The supplier has given the following code. But when I plug it into my application I get error messages that HTTP etc. is not defined. What do I need to do?

    Dim uri As String
    Dim SemiColonSepVins As String
    Dim tmp As System.Net.Http.HttpResponseMessage
    Dim result As System.Threading.Tasks.Task(Of String)
    Try
    	uri = "https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/"
    	SemiColonSepVins = "5XYKT3A12CG000000;3D7KU28C54G000000;"
    	Dim client = New HttpClient()
    	client.BaseAddress = New Uri(uri)
    	Dim nameValues As New Dictionary(Of String, String)
    	nameValues.Add("data", SemiColonSepVins)
    	nameValues.Add("format", "json")
    	Dim name = New FormUrlEncodedContent(nameValues)
    	client.DefaultRequestHeaders.Accept.Add(New System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"))
    	Dim token = New System.Threading.CancellationToken()
    	tmp = client.PostAsync(client.BaseAddress, name, token).Result
    	result = tmp.Content.ReadAsStringAsync()
    Catch ex As Exception
    	' error handling
    End Try

    Hi

    I could get it to compile by adding

    Imports System.Net.Http

    at the top of code. Some of the code you posted was fully qualified - some not.

    Alternatively, fully qualify where needed. e.g.

            Dim uri As String
            Dim SemiColonSepVins As String
            Dim tmp As Net.Http.HttpResponseMessage
            Dim result As Task(Of String)
            Try
                uri = "https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/"
                SemiColonSepVins = "5XYKT3A12CG000000;3D7KU28C54G000000;"
                Dim client = New Net.Http.HttpClient()
                client.BaseAddress = New Uri(uri)
                Dim nameValues As New Dictionary(Of String, String)
                nameValues.Add("data", SemiColonSepVins)
                nameValues.Add("format", "json")
                Dim name = New Net.Http.FormUrlEncodedContent(nameValues)
                client.DefaultRequestHeaders.Accept.Add(New Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"))
                Dim token = New Threading.CancellationToken()
                tmp = client.PostAsync(client.BaseAddress, name, token).Result
                result = tmp.Content.ReadAsStringAsync()
                Stop
            Catch ex As Exception
                Stop
            End Try


    Regards Les, Livingston, Scotland



    • Edited by leshay Monday, June 19, 2017 3:15 AM
    Monday, June 19, 2017 3:12 AM
  • Hi James,

    I try your code at my side, but I didn't get any errors, can you point out where you met this error or share one entire sample that can reproduce your issue here? It is beneficial for you to solve this issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 20, 2017 7:23 AM
    Moderator