Download contents of webpage with VB - vb.net

I am using Visual Basic 2010 Express and have found a way to read a file:
Dim byter = My.Computer.FileSystem.ReadAllBytes("C:/Documents and Settings/textfile.txt")
Can I do something similar if I want to read the contents of a website?

Try something like:
Dim uri as New Uri("http://thewebsite")
Dim request as HttpWebRequest = HttpWebRequest.Create(uri)
request.Method = WebRequestMethods.Http.Get
Dim response As HttpWebResponse = request.GetResponse()
Dim reader As New StreamReader(response.GetResponseStream())
Dim webpageContents As String = reader.ReadToEnd()
response.Close()

Related

Cannot deserialise the JSON array

I'm working with VB.Net and can't manage to display the following JSON file in a datagridview.
{"files":[
{"file": "Test.out", "linecount": "4"},{"file": "test1.out", "linecount": "41"},{"file": "NocheinTest.out", "linecount": "41"}
]}
Can anyone help me with that?
here is the complete code
Dim request As HttpWebRequest
Dim response As HttpWebResponse = Nothing
Dim reader As StreamReader
Dim url As String
url = "http://...."
request = DirectCast(WebRequest.Create(url), HttpWebRequest)
response = DirectCast(request.GetResponse(), HttpWebResponse)
reader = New StreamReader(response.GetResponseStream())
Dim s As String
s = reader.ReadToEnd
Dim files = JObject.Parse(s)("files").ToDictionary(Function(jt) jt("file").ToString(), Function(jt) Convert.ToInt32(jt("linecount")))
DataGridView1.DataSource = files.ToList()

Visual Basic convert string to audio (WAV) file and play

I'm using a VB win form and need to play a wav from string but I don't know how to do it.
Dim URl As String ="http://localhost/main.php?command=readdata"
Dim request As HttpWebRequest = HttpWebRequest.Create(URl)
request.Proxy = Nothing
request.UserAgent = "Test"
Dim response As HttpWebResponse = request.GetResponse
Dim responseStream As System.IO.Stream = response.GetResponseStream
Dim Reader As New System.IO.StreamReader(responseStream)
Dim data As String = Reader.ReadToEnd
Reader.Close()
Dim AudioData As String = Hex2String(data)
'here i wont play and save this data
You don't want to read the data as a string, since it's not a string. WAV data is binary data. It's meaningless as a string, and if you encode it as a string, you may lose some data in the process. So, if you aren't converting it to a string, you don't need the StreamReader at all. Just create a new file as a stream, and then copy all the bytes from the one stream to the other:
Dim url As String ="http://localhost/main.php?command=readdata"
Dim request As HttpWebRequest = HttpWebRequest.Create(url)
request.Proxy = Nothing
request.UserAgent = "Test"
Dim response As HttpWebResponse = request.GetResponse()
Using responseStream As Stream = response.GetResponseStream()
Using fileStream As New New FileStream("MyFile.wav", FileMode.CreateNew)
responseStream.CopyTo(fileStream)
End Using
End Using

Create SOAP request with vb.net

I'm trying to call a webservice in vb.net and I have followed this thread: WebService Send SOAP request and received response using visual Basic
These are the steps that I have followed:
I have already add the reference to the project (http://webservice_url?wsdl).
I have written the soap request in an xml file by hand (I attach the xml file as image here )
I have load the xml file into an string variable and call to the webservice like that:
Dim strDocumentoSoap As String
Dim document As XDocument = XDocument.Load("C:\myFile.xml")
strDocumentoSoap = document.ToString
Dim servicioWeb As svc.v79jIntegracionPerfilSesion = New svc.v79jIntegracionPerfilSesion
Dim sbLogin As New System.Text.StringBuilder
sbLogin.Append(strDocumentoSoap)
Dim hwrequest As System.Net.HttpWebRequest
hwrequest = System.Net.WebRequest.Create("http://myServiceURL/realizarPeticionPublicacionOSBUS?WSDL")
hwrequest.Method = "POST"
hwrequest.ContentType = "text/xml; charset=utf-8"
Dim byteData As Byte() = System.Text.Encoding.UTF8.GetBytes(sbLogin.ToString())
hwrequest.ContentLength = byteData.Length
Dim writer As Stream = hwrequest.GetRequestStream
writer.Write(byteData, 0, byteData.Length)
writer.Close()
Dim Response As System.Net.HttpWebResponse
Response = hwrequest.GetResponse
Dim responseStatus As String = Response.StatusDescription.ToString
Dim DataStream As Stream
Dim Reader As StreamReader
DataStream = Response.GetResponseStream()
Reader = New StreamReader(DataStream)
Dim SD2Request As String = Reader.ReadToEnd()
'SHOW THE RESPONSE INTO MSGBOX
MsgBox(SD2Request)
This works perfectly, but I have read in the internet that this practice is not recommended (hand made XML for SOAP), because there are other useful tools in .net.
Can anyone explain me other options or libraries for creating a soap request in VB.NET without creating the soapenvelope by hand.
Thank you very much in advance!
Marga

how to use API in Vb net

What is the best way to issue a http get in VB.net? I want to get the result of a request like
http://89.36.220.56/api.php?photo=urlphoto&point=numberlikes&socks=proxy
Photo = URL Photo
Point = Number
Socks = Proxy
Dim url = "http://89.36.220.56/api.php?photo=urlphoto&point=numberlikes&socks=proxy"
Dim request = DirectCast(WebRequest.Create(url), HttpWebRequest)
Dim response = request.GetResponse()
Dim receiveStream As Stream = response.GetResponseStream()
Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
Dim data = readStream.ReadToEnd()
response.Close()
readStream.Close()

Error trying to Create ASANA Project in VB.NET

I am getting an error:
400 Bad request
when trying to create a project via vb.net in asana.
Note: The ApiKey I am using works when I use it in other vb.net code to get the list of workspaces which is where I got my workspace ID.
Here is my code; I would be grateful for any insight...
Public Sub main()
Dim address As Uri
address = New Uri("https://app.asana.com/api/1.0/projects")
Dim ApiKey As String
ApiKey = "<my api key>"
Dim basicAuthenticationString As String
basicAuthenticationString = Convert.ToBase64String(New UTF8Encoding().GetBytes(ApiKey + ":"))
' Create the web request
Dim request As HttpWebRequest
request = DirectCast(WebRequest.Create(address), HttpWebRequest)
request.Headers("Authorization") = "Basic " & basicAuthenticationString
request.Method = "POST"
request.ContentType = "application/json"
Dim postData As String = "{""data"":[{""name"":""Randy Test Project"",""notes"":""Randy Test Project Notes"",""workspace"":5272875888767}]}"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentLength = byteArray.Length
Dim dataStream As Stream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
Dim response As HttpWebResponse = request.GetResponse()
Console.WriteLine(CType(response, HttpWebResponse).StatusDescription)
dataStream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As String = reader.ReadToEnd()
Console.WriteLine(responseFromServer)
reader.Close()
dataStream.Close()
response.Close()
Exit Sub
End Sub
I was able to figure it out, My address needed to be:
Dim address As Uri = New Uri("app.asana.com/api/1.0/teams/22956925957833/projects")
Then my postData needed to be:
Dim postData As String = "{""data"":{""name"":""Randy Test Project"",""notes"":""Randy Test Project Notes""}}"
Alternatively, you can specify the team or workspace in the post data. When you get a 400 Bad Request the body of the error response will tell you which fields were actually missing/invalid.