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
Here my code so far... to Google.... Can anyone help?
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: