Resolved WinForm API call to return result

divjoy

Well-known member
Joined
Aug 25, 2013
Messages
159
Programming Experience
1-3
HI,

I'm testing an API call from a Winforms, I'm using VS 2013. I've been reading a lost about it and was using an API call to a google website to test, but its very confusing, normally I can figure out what happening and get it going but its all very confusing between the imports the events, json and handling the result etc nothing makes sense.

I was trying to call an api to a Dads jokes website to test too without success!
Here the api breakdown..

The current version of the API lives at https://us-central1-dadsofunny.cloudfunctions.net/DadJokes

GET /random/jokes
Returns a joke object that contains a setup, punchline, type and id​

Here my code so far... to Google.... Can anyone help?
VB.NET:
Imports System.IO

Imports System.Net

Imports System.Text



'Add reference under project ver 4.0.0.0

Imports System.Net.Http



Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        ' Create new Task.

        ' ... Use AddressOf to reference a method.

        Dim t As Task = New Task(AddressOf DownloadPageAsync)

        ' Start the task.

        t.Start()

        ' Print a message as the page downloads.

        Console.WriteLine("Downloading page...")

        Console.ReadLine()

    End Sub



    Async Sub DownloadPageAsync()





        Dim page As String = "Wikipedia, the free encyclopedia"        ' Use HttpClient in Using-statement.

        ' ... Use GetAsync to get the page data.

        Using client As HttpClient = New HttpClient()

            Using response As HttpResponseMessage = Await client.GetAsync(page)

                Using content As HttpContent = response.Content

                    ' Get contents of page as a String.

                    Dim result As String = Await content.ReadAsStringAsync()

                    ' If data exists, print a substring.

                    If result IsNot Nothing And result.Length > 50 Then

                        Me.TextBox1.Text = (result.Substring(0, 50) + "...")

                    End If

                End Using

            End Using

        End Using

    End Sub







    Private Function SendRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String

        Dim response As String

        Dim request As WebRequest



        request = WebRequest.Create(uri)

        request.ContentLength = jsonDataBytes.Length

        request.ContentType = contentType

        request.Method = method



        Using requestStream = request.GetRequestStream

            requestStream.Write(jsonDataBytes, 0, jsonDataBytes.Length)

            requestStream.Close()



            Using responseStream = request.GetResponse.GetResponseStream

                Using reader As New StreamReader(responseStream)

                    response = reader.ReadToEnd()

                End Using

            End Using

        End Using



        Return response

    End Function
 
Last edited:
At 130 posts you should know this:
insertcode.png
 
Dim page As String = "Wikipedia, the ..."
Await client.GetAsync(page)
It returns Json data so install the nuget package Newtonsoft.Json first.
Since you know HttpClient I'll post an example to use the json result:
VB.NET:
Private Async Sub Button2_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim address = "https://us-central1-dadsofunny.cloudfunctions.net/DadJokes/random/jokes"
    Using client As New Net.Http.HttpClient(), result = Await client.GetAsync(address)
        If result.IsSuccessStatusCode Then
            Dim content = Await result.Content.ReadAsStringAsync
            Dim j = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Joke)(content)
            TextBox1.Lines = {j.Type, j.Setup, j.Punchline}
        Else
            TextBox1.Text = "error downloading"
        End If
    End Using
End Sub

Class Joke
    Public Property Id As String
    Public Property Type As String
    Public Property Setup As String
    Public Property Punchline As String
End Class
 
It returns Json data so install the nuget package Newtonsoft.Json first.
Since you know HttpClient I'll post an example to use the json result:
VB.NET:
Private Async Sub Button2_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim address = "https://us-central1-dadsofunny.cloudfunctions.net/DadJokes/random/jokes"
    Using client As New Net.Http.HttpClient(), result = Await client.GetAsync(address)
        If result.IsSuccessStatusCode Then
            Dim content = Await result.Content.ReadAsStringAsync
            Dim j = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Joke)(content)
            TextBox1.Lines = {j.Type, j.Setup, j.Punchline}
        Else
            TextBox1.Text = "error downloading"
        End If
    End Using
End Sub

Class Joke
    Public Property Id As String
    Public Property Type As String
    Public Property Setup As String
    Public Property Punchline As String
End Class
Thank You JohnH I'm looking forward to trying....kind regards
 
Hi JohnH,

That worked a treat, still not sure how it all works tbh.

Will the same code work with google api, I am planning now on passing a list of postcodes to google maps to get the road distance between them all !

I am also wondering if I setup a databse in Azure would it be safe and secure to use winform forntend to access, I knnow Azure has ACL but I woudl also need to dpely encryption.

I know a lot of people are doing it through webpage, but Winform is so much faster for development?

kind regards
 
You should create separate threads for those two topics.
 

Latest posts

Back
Top