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...
Related
Getting error / crashed when internet connection lost in visual basic 2008.
Public Function GetIP() As String
Dim uri_val As New Uri("http://example.com/")
Dim request As HttpWebRequest = HttpWebRequest.Create(uri_val)
request.Method = WebRequestMethods.Http.Get
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(response.GetResponseStream())
Dim myIP As String = reader.ReadToEnd()
response.Close()
Return myIP
End Function
So i have written code to allow me to download files from an FTP server using the system.io.stream function in vb.net. Now I need to be able to find the length of the stream, when i has finished.
I have worked with streamreader in the past but this stream doesn't work in the same way so i'm not sure how to proceed.
Try
Dim request As System.Net.FtpWebRequest
request = DirectCast(System.Net.WebRequest.Create("ftp://ftp.server.com/Folder/" & FileName & ".pdf"), System.Net.FtpWebRequest)
request.Credentials = New System.Net.NetworkCredential("Username", "Password")
request.Method = System.Net.WebRequestMethods.Ftp.DownloadFile
Dim stream As System.IO.Stream = request.GetResponse.GetResponseStream
'Dim OutPutFilepath As String = "DownloadTest" & "\" & IO.Path.GetFileName("ftp://ftp.Server.com/Folder/")
output = System.IO.File.Create("C:\Users\ASUS\Documents\TestFile2.pdf")
output.Close()
stream.Close()
Console.WriteLine("Downloaded")
Console.ReadLine()
Catch ex As Exception
MsgBox(ex.Message)
End Try
Also, I have tried using the stream.length feature however, i got an exception saying something along the lines of "this stream does not support seek functions".
Thanks in advance
When using the following code, if I specify a wrong username/password combination I get a 407 error.
If I use a correct one, I get the page requested.
If I try again with a wrong username/password I get a normal response.
For some reason, WebRequest caches the proxy's username/password (CredentialCache) combination for the requested URL. If I change the URL pointing to a different domain, I get a proper 407.
How can I stop WebRequest from using last successful credential?
Dim request As WebRequest = WebRequest.Create("http://contoso.com")
request.Proxy = New WebProxy("http://myproxy:8080")
Dim username = InputBox("Username")
Dim password = InputBox("Password")
request.Proxy.Credentials = New NetworkCredential(username, password)
Try
Using response = request.GetResponse()
' Display the status.
txt_response.Text = CType(response, HttpWebResponse).StatusDescription & "Response is from cache?: " & response.IsFromCache
' Get the stream containing content returned by the server.
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()
' Display the content.
txt_Result.Text = responseFromServer
' Clean up the streams and the response.
reader.Close()
response.Close()
End Using
Catch ex As WebException
txt_response.Text = ex.Status & ": " & ex.Message
End Try
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
My app currently sends GET httpwberequests and parses the content after reading the response stream for each request and the repeats this process over and over. I need to implement a continuous http stream which uses the same GET request but the connection stays open, from what I understand. how would I change my existing code to do this?
Dim responseReader As StreamReader = Nothing
Dim Reader As StreamReader = Nothing
Dim responseData As String = ""
Dim webresponse As HttpWebResponse = Nothing
Dim responseStream As Stream = Nothing
Dim returnContent As String = Nothing
Dim cdata As Integer = 0
webRequest = TryCast(System.Net.WebRequest.Create(url), HttpWebRequest)
Try
webresponse = DirectCast(webRequest.GetResponse(), HttpWebResponse)
responseStream = webresponse.GetResponseStream
If webresponse.ContentEncoding.ToLower().Contains("gzip") Then
responseStream = New GZipStream(responseStream, CompressionMode.Decompress)
ElseIf webresponse.ContentEncoding.ToLower().Contains("deflate") Then
responseStream = New DeflateStream(responseStream, CompressionMode.Decompress)
End If
Reader = New StreamReader(responseStream, Encoding.Default)
responseData = Reader.ReadToEnd()
Catch
Throw
Finally
webRequest.GetResponse().GetResponseStream().Close()
Reader.close()
End Try
Return responseData
The streaming HTTP implementation allows one to cosume data in real time over a single persistent HTTp request. It access a data feed that continues to send activities on that response. I need to read data from the response, process it and then continue reading from the response and continue this process until the connection is closed.
Would this be like a asynchronous request instead?
This is actually one of the goals of HTML5. You can take a look at http://socket.io/ to see implementations that work for HTML4 browsers