Taking Json from API and iterating over the results - vb.net

So I feel stupid for asking probably a easy question but I'm not very familiar with .NET and I've been googling for a while now.
I'm looking to take in data from a web API and be able to iterate over it. The data looks like this
[
{"id":"5", "date":"01-01-2014"},
{"id":"90", "date":"05-01-2013"}
]
What I've got so far:
Private Sub newGetData()
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Try
request = DirectCast(WebRequest.Create("http://somesite.com"), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim rawresp As String
rawresp = reader.ReadToEnd()
Dim jResults As JObject = JObject.Parse(rawresp)
Dim results As List(Of JToken) = jResults.Children().ToList
For Each item As JProperty In results
item.CreateReader()
MsgBox(item.name)
Next
'usernameTextbox.text = jResults("name").ToString()
'placenameTextbox.text = jResults("place")("name").ToString()
Catch ex As Exception
MsgBox(ex.ToString)
Finally
If Not response Is Nothing Then response.Close()
End Try
End Sub
I've tried a bunch of things but it always fails on the loop. How do I go about doing this?
EDIT: The error message for this one
Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader)
at Newtonsoft.Json.Linq.JObject.Parse(String json)
at Forecasting.Form1.newGetData() in

Related

Problem with "try-catch" in a web request vb.net

I'm making a web request that completes successfully most of the time (target$ is a URL). But occasionally my code throws a valid exception, 404 not found, if the URL target$ doesn't exist, and execution stops. The code:
Sub scrape(target$)
Dim request As WebRequest = WebRequest.Create(target$)
Dim response As WebResponse = request.GetResponse()
Dim dataStream As Stream = response.GetResponseStream()
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
txtResponse.Text = ""
txtResponse.Text = responseFromServer
' Clean up the streams and the response.
reader.Close()
response.Close()
end sub
The exception, if thrown, happens in the second line, "Dim response...". So I tried adding a "try-catch" as shown.
Sub scrape(target$)
Dim request As WebRequest = WebRequest.Create(target$)
Dim response As WebResponse = request.GetResponse()
Try
Dim dataStream As Stream = response.GetResponseStream()
Catch
exflag = True
End Try
' Open the stream using a StreamReader for easy access.
Dim reader As New StreamReader(dataStream)
' Read the content.
Dim responseFromServer As String = reader.ReadToEnd()
txtResponse.Text = ""
txtResponse.Text = responseFromServer
' Clean up the streams and the response.
reader.Close()
response.Close()
end sub
But now when I try to compile the code, VisualStudio tells me that "datastream is not declared" and the compile fails.
What am I doing wrong and how do I catch the exception when it's thrown?
Thanks...

Send a POST request with vb.net gives 400 bad request error

I am not familiar a lot with Vb.Net, but I try to tweak something on an existent project. I have a cURL that try to implement on Vb.Net. I found different answers here and in other forums, but this way is the one I managed to reach
Private Function SendRequest(uri As Uri, jsonDataBytes As Byte(), contentType As String, method As String) As String
Dim req As WebRequest = WebRequest.Create(uri)
req.ContentType = contentType
req.Method = method
req.ContentLength = jsonDataBytes.Length
Dim stream = req.GetRequestStream()
stream.Write(jsonDataBytes, 0, jsonDataBytes.Length)
stream.Close()
Dim response = req.GetResponse().GetResponseStream()
Dim reader As New IO.StreamReader(response)
Dim res = reader.ReadToEnd()
reader.Close()
response.Close()
Return res
End Function
Dim postData As String = String.Format("text={0}", title)
Dim data = Encoding.UTF8.GetBytes(postData)
Dim uri = New Uri("https://.....")
Dim slackResponse = SendRequest(uri, data, "application/json", "POST")
And this is the error I get:
Exception Details: System.Net.WebException: The remote server returned an error: (400) Bad Request.
If I comment out the SendRequest function, I got an error during calling that, so I guess it is on that part.
Not able to debug more. Any ideas?
In order to debug more, you need to catch the WebException. WebExceptions have a response object that may contain more information.
Dim response as WebResponse
Try
response = req.GetResponse().GetResponseStream()
Catch ex As Net.WebException
If ex.Response IsNot Nothing Then
response = ex.Response
End If
End Try
This could be a problem with your URL parameters but many sites also return this class of error when there is a problem in the json content of the request.
I solved this long time ago by adding.
request.ContentType = "application/x-www-form-urlencoded"

vb.net OpenWeatherMap using HttpWebRequest

I am unable to get the HttpWebRequest to work properly with OpenWeatherMap.
When I try out the URL from the browser I get the data. However, when I am sending it from the program I'm getting a message with code id. Like this:
"message":"","cod":"404"
What Am I doing wrong?
VB.NET Code:
Private Shared AppID As String = "add_app_id_Here"
Public Shared Function GetWeather(ByVal location As String) As List(Of WeatherDetails)
Dim url = String.Format _
("http://api.openweathermap.org/data/2.5/forecast/daily?q={0}&type=accurate&mode=xml&units=metric&cnt=3&appid={1}",
location, AppID)
Try
Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create(url), HttpWebRequest)
request.AuthenticationLevel = Net.Security.AuthenticationLevel.None
Dim response As WebResponse = request.GetResponse()
'The body of the request is sent here
Dim responseReader As New StreamReader(response.GetResponseStream())
Dim responseInfo As String = responseReader.ReadToEnd()
responseReader.Close()
response.Close()
If Not (responseInfo.Contains("message") And responseInfo.Contains("cod")) Then
Dim xEl = XElement.Load(New System.IO.StringReader(responseInfo))
Return GetWeatherInfo(xEl)
Else
Return New List(Of WeatherDetails)
End If
Catch ex As Exception
Return New List(Of WeatherDetails)
End Try
End Function
After breaking my head I found that his method HttpWebRequest is not tolerant to special characters or non printable characters.
Thus in the URL I had to hardcode the "%27" character and it solved the problem.

How to get result from failing webclient.downloadstring?

If we use webclient.downloadstring on a server with error, we got nothing
Try
result = webClient.DownloadString("https://api.bodis.com/domainclassification?domain=" + CurrentBlog.Domain)
Catch ex As Exception
System.Windows.Forms.Application.DoEvents()
End Try
Usually result contains the response. But in case of error result is empty.
I want to know something. For example, even if the result is bad, usually there is a custom 404 message, etc.
The api.bodis.com is problematic because it mix secure and non secure stuff. However, it gives output anyway in internet explorer. I want to use webclient and I want to know the ouput
Getting ex.response yields nothing
Dim adult = webClient.DownloadString("http://googleads.g.doubleclick.net/apps/domainpark/domainpark.cgi?callback=_google_json_callback&output=js&client=ca-dp-godaddy2_xml&domain_name=" + CurrentBlog.Domain)
'ias.NavigateTillComplete("https://api.bodis.com/domainclassification?domain=" + CurrentBlog.Domain)
Dim result = ""
Dim url = "http://api.bodis.com/domainclassification?domain=" + CurrentBlog.Domain
Dim webRequest = DirectCast(System.Net.HttpWebRequest.Create(url), System.Net.HttpWebRequest)
Dim response As HttpWebResponse
Try
response = DirectCast(webRequest.GetResponse(), HttpWebResponse)
Catch ex As System.Net.WebException
System.Windows.Forms.Application.DoEvents()
response = DirectCast(ex.Response, HttpWebResponse) 'is nothing there
End Try
Dim responseStream = response.GetResponseStream

VB.net how to make stream reader ignore some line?

i am using a stream reader to get the HTML of some page, but there are lines that i want to ignore, such as if a line starts with <span>
any advice?
Here is my function
Public Function GetPageHTMLReaderNoPrx(ByVal address As Uri) As StreamReader
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Try
request = DirectCast(WebRequest.Create(address), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
Select Case CType(response, Net.HttpWebResponse).StatusCode
Case 200
reader = New StreamReader(response.GetResponseStream(), Encoding.Default)
Case Else
MsgBox(CType(response, Net.HttpWebResponse).StatusCode)
End Select
Catch
If Not response Is Nothing Then response.Close()
End Try
Return reader
End Function
this is how the HTML looks like
<tr>Text
<span>show all</span>
</tr>
If you insist on using strings, you could do something like this:
Do
Dim line As String = reader.ReadLine()
If line Is Nothing Then Exit Do 'end of stream
If line.StarsWith("<span>") Then Exit Do 'ignore this line
'otherwise do some processing here
'...
Loop
But this approach is not stable - any minor change in the input HTML can break your flow.
More elegant solution would be using XElement:
Dim xml = <tr>Text
<span>show all</span>
</tr>
xml.<span>.Remove()
MsgBox(xml.Value.Trim)