I am trying to retrieve data from webpage. On the webpage, there is only sentence. What is the correct way to retrieve the sentence ? I have tried this without any luck
Dim webClient As New System.Net.WebClient
Dim result As String = webClient.DownloadString("http://example.org")
MsgBox(result)
OK, I think you're not getting the results from your HTTP call. Here's what I did - sorry it's C#, you'll have to translate but this is probably close to what you need:
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader rdr = new StreamReader(receiveStream, Encoding.UTF8);
try
{
//Check that page downloaded OK
if (response.StatusCode == HttpStatusCode.OK)
{
//Do something
}
}
catch (Exception xx)
{
//handle error
}
finally
{
response.Close();
}
I used to do it with this:
Imports System.Net
Imports System.IO
Imports System.IO.Compression
And:
Dim urlUri As New System.Uri("http://www.example.org/")
Dim strResult as string = String.Empty
Dim webHTTP As HttpWebRequest
Try
webHTTP = WebRequest.Create(urlUri)
webHTTP.KeepAlive = False
Catch ex As Exception
Throw New Exception("An error occured while trying to create the request:" & vbCrLf & ex.Message)
Exit Sub
End Try
Dim WebResponse As HttpWebResponse
Try
WebResponse = webHTTP.GetResponse()
Catch ex As Exception
Throw New Exception("An error occured while retrieving the response:" & vbCrLf & ex.Message)
Exit Sub
End Try
Dim responseStream As Stream
Try
responseStream = WebResponse.GetResponseStream()
Catch ex As Exception
Throw New Exception("An error occured while retrieving the response stream:" & vbCrLf & ex.Message)
Exit Sub
End Try
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
Dim CHUNK_SIZE As Long = 3072
Dim read(CHUNK_SIZE) As [Char]
Dim reader As StreamReader
Try
reader = New StreamReader(responseStream, Encoding.Default)
Catch ex As Exception
Throw New Exception("An error occured while creating the streamreader:" & vbCrLf & ex.Message)
Exit Sub
End Try
Dim count As Integer = reader.Read(read, 0, CHUNK_SIZE)
Do While count > 0
Dim str As New [String](read, 0, count)
strResult &= str
ReDim read(CHUNK_SIZE)
count = reader.Read(read, 0, CHUNK_SIZE)
Loop
reader.Close()
responseStream.Close()
WebResponse.Close()
Messagebox.Show(strResult)
Related
I need to send a file that I receive in 'testFile1' to 'testFile2'. I don't know if the stream I am creating is the right way to do it or if I have to retrieve the file on 'testFile2' in another way. Any help?
<HttpPost>
<Route("testFile1")>
Function postTestFile1() As IHttpActionResult
Try
Dim files = HttpContext.Current.Request.Files
If files.Count = 0 Then Throw New Exception("No file")
Dim file = files(0)
Dim req As HttpWebRequest = WebRequest.Create("http://localhost:9000/api/test/testFile2")
req.Method = "POST"
req.ContentType = "multipart/form-data"
req.ContentLength = file.ContentLength
Dim stream = req.GetRequestStream
Dim fileData As Byte()
Using BinaryReader As New BinaryReader(file.InputStream, Encoding.UTF8)
fileData = BinaryReader.ReadBytes(file.ContentLength)
End Using
stream.Write(fileData, 0, fileData.Length)
Dim response As WebResponse = req.GetResponse
Catch ex As Exception
Return InternalServerError()
End Try
End Function
<HttpPost>
<Route("testFile2")>
Function postTestFile2() As IHttpActionResult
Try
Dim files = HttpContext.Current.Request.Files
Dim requestStream = HttpContext.Current.Request.InputStream
If files.Count = 0 Then Throw New Exception("No file")
Dim file = files(0)
'Do the necessary with the file
Return Ok()
Catch ex As Exception
Return InternalServerError()
End Try
End Function
It appears that I am able to get the temp-token, but when I try to convert to a permanent token I get "BAD REQUEST". Any help would be appreciated.
Code is hooked to WebBrowser1.Navigated
Dim strTempToken As String = ""
If WebBrowser1.Url.ToString.ToUpper.StartsWith("HTTPS://GO.SERVICEM8.COM/DASHBOARD?&S_AUTH=") Then
Try
Dim arrURL As Array = Split(WebBrowser1.Url.ToString, "&")
strTempToken = arrURL(1).ToString.Replace("s_auth=", "")
Catch ex As Exception
MessageBox.Show("Fail-1." & ex.Message)
End Try
If strTempToken.Length > 0 Then
Try
Dim hc As New System.Net.Http.HttpClient
Dim req As New System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, "https://www.servicem8.com/oauth/access_token")
req.Headers.Add("client_id", strClientID)
req.Headers.Add("client_secret", strClientSecret)
req.Headers.Add("code", strTempToken) '
Dim res As System.Net.Http.HttpResponseMessage = Await hc.SendAsync(req)
My.Computer.FileSystem.WriteAllText(strKeyPath & "servicem8-perm-key.txt", res.RequestMessage.RequestUri.ToString, False)
MessageBox.Show("Success")
WebBrowser1.Url = New Uri("")
Catch ex As Exception
MessageBox.Show("Fail-2." & ex.Message)
End Try
End If
End If
I've looked around and couldn't find an answer so I thought i'd ask it here.
I have a program the calls an API with login info passed in the header and the server returns some info upon authentication. The problem is if the user creds are incorrect, the program crashes and throws an exception. The error is 401. How do it catch this, and tell the user?
My code is:
Dim request = TryCast(System.Net.WebRequest.Create("https://myurl.com"), System.Net.HttpWebRequest)
request.Method = "GET"
request.Headers.Add("authorization", "Bearer " + AccessToken)
request.ContentLength = 0
Dim responseContent As String
Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse)
Using reader = New System.IO.StreamReader(response.GetResponseStream())
responseContent = reader.ReadToEnd()
MessageBox.Show(responseContent)
End Using
End Using
Thanks.
Use a try catch statement
try
'My error prone code
catch ex as Exception
'Handle my error
end try
Try
Dim request = TryCast(System.Net.WebRequest.Create("https://myurl.com"), System.Net.HttpWebRequest)
request.Method = "GET"
request.Headers.Add("authorization", "Bearer " + AccessToken)
request.ContentLength = 0
Dim responseContent As String
Using response = TryCast(request.GetResponse(), System.Net.HttpWebResponse)
Using reader = New System.IO.StreamReader(response.GetResponseStream())
responseContent = reader.ReadToEnd()
MessageBox.Show(responseContent)
End Using
End Using
Catch ex As Exception
MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
I am getting above error while deserializating BinaryFile to Object. I have tried set position=0, seek.begin method and tried with memory stream but no use. Every time i am getting same issue. Can anybody help me out?. Below is my code snippet for serialization and deserialization
Serialization
Dim fs As New FileStream(Filename, FileMode.OpenOrCreate)
Try
'fs = New FileStream(Filename, FileMode.OpenOrCreate)
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
bf.AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full
bf.TypeFormat = Runtime.Serialization.Formatters.FormatterTypeStyle.TypesWhenNeeded
bf.Serialize(fs, data)
Catch ex As Exception
MsgBox("Exception: " & ex.Message & " | Stacktrace: " & ex.StackTrace)
Finally
fs.Flush()
fs.Close()
fs.Dispose()
End Try
Deserialization
Dim fs As FileStream = Nothing
Try
fs = IO.File.OpenRead(Filename)
'fs = New FileStream(Filename, FileMode.Open)
Dim bf As New Runtime.Serialization.Formatters.Binary.BinaryFormatter()
bf.AssemblyFormat = Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full
bf.TypeFormat = Runtime.Serialization.Formatters.FormatterTypeStyle.TypesWhenNeeded
fs.Seek(0, SeekOrigin.Begin) 'fs.Position=0
Dim obj As Object = bf.Deserialize(fs)
Return obj
Catch ex As Exception
MsgBox("There was an exception while trying to convert binary file to object. Exception: " & ex.Message & " | Stacktrace: " & ex.StackTrace)
Finally
If fs IsNot Nothing Then
fs.Flush()
fs.Close()
fs.Dispose()
End If
End Try
I want to download all file that located on FTP site. I break into 2 operations. First is to get the list of all files in the directory. Then I proceed to second which download each file. But unfortunately, the downloaded file is corrupt. Worst over, it affect the file on FTP folder. Both folder contains corrupt file. From a viewing thumbnails image to default image thumbnails. How this happen and to overcome this? Below are my code; full code class. I provide as reference and guide to do a correct way :)
Private strTargetPath As String
Private strDestPath As String
Private Sub frmLoader_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
objSQL = New clsSQL
If objSQL.SQLGetAllData Then 'Get data from SQL and insert into an arraylist
strTargetPath = "ftp://192.168.1.120/image/"
strDestPath = "D:\Application\FTPImg\"
ListFileFromFTP()
End If
Catch ex As Exception
strErrMsg = "Oops! Something is wrong with loading login form."
MessageBox.Show(strErrMsg & vbCrLf & "ExMsg: " & ex.Message, "Error message!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Private Sub ListFileFromFTP()
Dim arrFile As ArrayList
Dim strPath As String
Try
Dim reqFTP As FtpWebRequest
reqFTP = FtpWebRequest.Create(New Uri(strTargetPath))
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential("user", "user123")
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory
reqFTP.Proxy = Nothing
reqFTP.KeepAlive = False
reqFTP.UsePassive = False
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
Dim sr As StreamReader
sr = New StreamReader(reqFTP.GetResponse().GetResponseStream())
Dim FileListing As String = sr.ReadLine
arrFile = New ArrayList
While FileListing <> Nothing
strPath = strTargetPath & FileListing
arrFile.Add(strPath)
FileListing = sr.ReadLine
End While
sr.Close()
sr = Nothing
reqFTP = Nothing
response.Close()
For i = 0 To (arrFile.Count - 1)
DownloadImage(arrFile.Item(i))
Next
Catch ex As Exception
strErrMsg = "Oops! Something is wrong with ListFileFromFTP."
MessageBox.Show(strErrMsg & vbCrLf & "ExMsg: " & ex.Message, "Error message!", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
Public Function DownloadImage(ByVal strName As String) As String
Dim ftp_request As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Dim strOldName As String
Dim strNewName As String
Dim withoutextension As String
Dim extension As String
Try
strOldName = strTargetPath & strName
withoutextension = Path.GetFileNameWithoutExtension(strOldName)
extension = Path.GetExtension(strOldName)
strNewName = strDestPath & withoutextension & extension
ftp_request = CType(System.Net.FtpWebRequest.Create(strName), System.Net.FtpWebRequest)
ftp_request.Method = System.Net.WebRequestMethods.Ftp.UploadFile
ftp_request.Credentials = New System.Net.NetworkCredential("user", "user123")
ftp_request.UseBinary = True
ftp_request.KeepAlive = False
ftp_request.Proxy = Nothing
Dim response As FtpWebResponse = DirectCast(ftp_request.GetResponse(), FtpWebResponse)
Dim responseStream As IO.Stream = response.GetResponseStream
Dim fs As New IO.FileStream(strNewName, IO.FileMode.Create)
Dim buffer(2047) As Byte
Dim read As Integer = 0
Do
read = responseStream.Read(buffer, 0, buffer.Length)
fs.Write(buffer, 0, read)
Loop Until read = 0
responseStream.Close()
fs.Flush()
fs.Close()
responseStream.Close()
response.Close()
Catch ex As WebException
Dim response As FtpWebResponse = ex.Response
If response.StatusCode = FtpStatusCode.ActionNotTakenFileUnavailable Then
MsgBox(response.StatusDescription)
Return String.Empty
Else
MsgBox(response.StatusDescription)
End If
End Try
End Function
For download file, this is the right code that succeed:
Private Sub Download(ByVal strFTPPath As String)
Dim reqFTP As FtpWebRequest = Nothing
Dim ftpStream As Stream = Nothing
Try
Dim outputStream As New FileStream(strDestPath & strImgName, FileMode.Create)
reqFTP = DirectCast(FtpWebRequest.Create(New Uri(strFTPPath)), FtpWebRequest)
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile
reqFTP.UseBinary = True
reqFTP.Credentials = New NetworkCredential("user", "user123")
Dim response As FtpWebResponse = DirectCast(reqFTP.GetResponse(), FtpWebResponse)
ftpStream = response.GetResponseStream()
Dim cl As Long = response.ContentLength
Dim bufferSize As Integer = 2048
Dim readCount As Integer
Dim buffer As Byte() = New Byte(bufferSize - 1) {}
readCount = ftpStream.Read(buffer, 0, bufferSize)
While readCount > 0
outputStream.Write(buffer, 0, readCount)
readCount = ftpStream.Read(buffer, 0, bufferSize)
End While
ftpStream.Close()
outputStream.Close()
response.Close()
Catch ex As Exception
If ftpStream IsNot Nothing Then
ftpStream.Close()
ftpStream.Dispose()
End If
Throw New Exception(ex.Message.ToString())
End Try
End Sub
There is something strange in your code. You are closing the responseStream twice.
And you are using ftp request method uploadFile. But then downloading it.
Instead of using the ftp stuff use this instead. Its much more simple and your outsourcing the buffers and streams to microsoft, which should be less buggy than us.
Dim webClient = new WebClient()
webClient.Credentials = new NetworkCredential("user", "user123")
dim response = webClient.UploadFile("ftp://MyFtpAddress","c:\myfile.jpg")
dim sResponse = System.Text.Encoding.Default.GetString(response)