VB.net loading out of date webpages - vb.net

I use the following code to copy a certain line of text from the website listed.
'Track A
Dim TrackA As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://pilotweb.nas.faa.gov/common/nat.html")
Dim GaA As System.Net.HttpWebResponse = TrackA.GetResponse
Dim WrA As System.IO.StreamReader = New System.IO.StreamReader(GaA.GetResponseStream)
Dim ContentStrA As String = WrA.ReadToEnd
Dim StartIndexA As Integer = ContentStrA.IndexOf(vbLf & "A ") + 1
Dim StrLengthA As Integer = ContentStrA.IndexOf(vbLf, StartIndexA) - StartIndexA
However when I open the link in a tab in VB, it shows a version of the site from a few days ago, how do I prevent this from happening as I need the current version of the site?

Change the Cache Policy of your System.Net.HttpWebRequest object, so that you're not getting cached results.
Alternatively, you can clear the cache for your URL.

Related

Retrieving username and password from Dropbox file

I have a login tool which grabs the username and password from a file in my dropbox account.
Dim prequest As HttpWebRequest = HttpWebRequest.Create("http://dl.dropbox.com/s/jb4m5udkt96a43z/premuim.txt")
Dim presponse As HttpWebResponse = prequest.GetResponse()
Dim psr As IO.StreamReader = New IO.StreamReader(presponse.GetResponseStream())
Dim premium As String = psr.ReadToEnd()
That's the code I am using, it downloads the file and gives me the text inside. So I used this about half a year ago and it worked fine, now I am trying to use it but it doesn't work anymore.

Need help getting Registry.GetValue to work correctly

I have a simple setup project that is no longer working and it seems like a windows update is the cause. I am using Visual Studio 2010 on Windows 7. The project is 64bit. It still works on some computers but it does not work on any computers that have had updates recently.
Here is the original code:
Dim appPath As String = Registry.GetValue("HKEY_CURRENT_USER\SOFTWARE\Our Company Inc.\SoftwareName.exe", "Path", "Not Found")
appPath &= "Colorbar.col"
Dim sid : sid = "S-1-1-0"
Dim objWMI : objWMI = GetObject("winmgmts://./root\cimv2")
Dim objSID : objSID = objWMI.Get("Win32_SID='" & sid & "'")
Dim userAccount As String = objSID.AccountName
Dim fileInfo As IO.FileInfo = New IO.FileInfo(appPath)
Dim fileAcl As New FileSecurity
fileAcl.AddAccessRule(New FileSystemAccessRule(userAccount, FileSystemRights.FullControl, AccessControlType.Allow))
fileInfo.SetAccessControl(fileAcl)
I have put the key value pair of "Path" and "[TARGETDIR]" in the registry editor and have the output from this installer class (the code above) in the Install and Commit custom actions.
This code that used to work now returns "Exception has been thrown by the target of an invocation -> C:\Windows\SYSWOW64\Colorbar.col"
I have checked the registry when this message appears and the path is correct so I don't know where SYSWOW64 is coming from.
I have tried to change getting the appPath using this code:
Dim regKey As RegistryKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, RegistryView.Registry64)
regKey = regKey.OpenSubKey("SOFTWARE\Our Company Inc.\SoftwareName.exe")
Dim appPath As String = regKey.GetValue("Path").ToString
This returns an "Object reference not set to an instance of an object" error.
I have made a test Windows Form application and put both versions of code in a button event. Everything works as expected. Any ideas why the code does not work in a setup project anymore and what I can do to get it working again?
Thanks in advance.
Finally have it working again. I got around using the registry by using the custom action's CustomActionData and setting it to:
/name="[TARGETDIR]\"
I was then able to access it in my installer class by using this line of code:
Dim appPath As String = Context.Parameters.Item("name")
Once the path was set, everything else worked as expected. The final code looks like this:
Public Overrides Sub Commit(ByVal stateSaver As System.Collections.IDictionary)
MyBase.Commit(stateSaver)
Dim appPath As String = Context.Parameters.Item("name")
appPath = appPath.Remove(appPath.Length - 1)
appPath &= "Colorbar.col"
Dim sid : sid = "S-1-1-0"
Dim objWMI : objWMI = GetObject("winmgmts://./root\cimv2")
Dim objSID : objSID = objWMI.Get("Win32_SID='" & sid & "'")
Dim userAccount As String = objSID.AccountName
Dim fileInfo As IO.FileInfo = New IO.FileInfo(appPath)
Dim fileAcl As New FileSecurity
fileAcl.AddAccessRule(New FileSystemAccessRule(userAccount, FileSystemRights.FullControl, AccessControlType.Allow))
fileInfo.SetAccessControl(fileAcl)
End Sub

unable to run Exchange Powershell through vb.net application

So I'm going round in circles trying to get this to work, I've been trying for two days and I just can't figure it out.
I have the following vb function that takes a created powershell script, and should run it in powershell. Everything works fine, until the point at which the command pipeline is invoked. At this point, no commands run.
As you can see, I have tried to add the Microsoft.Exchange.Management.PowerShell.E2010 snapin to the runspace, it didn't like that at all stating something along the lines of the snapin didnt exist (which it does), and also when I run the code as shown, no commands are recognised as valid. I even added the specific command "Add-PSSnapin" to try and load any Exchange snapins, but it states that "Add-PSSnapin" is not recognised as a valid command.
If I pause the program just before the commands are involked, I can see every command within the pipeline, in the correct format. If I copy and paste the command text in the pipeline directly into a powershell window, it runs fine.
My code is below, any suggestions welcome.
edit: I have also tried adding the line "Add-PSSnapin Ex" (with an asterisk each side of Ex - I cant figure the formatting out on this, sorry)
to try and load the Exchange PS Snapins as the first thing the script would run (opposed to setting this up in the runspace) but no luck
Private Function scriptRunner(ByVal scripttorun As String) As String
Dim initial As InitialSessionState = InitialSessionState.CreateDefault()
Dim result As String = ""
Dim lineFromScript As String = ""
Dim reader As New StreamReader(tempScript)
Dim rsConfig As RunspaceConfiguration = RunspaceConfiguration.Create()
Dim snapInException As New PSSnapInException
Dim strUserName As String = "DOMAIN\USER"
Dim strPassword As String = "PASSWORD"
Dim SecuredPSWD As New System.Security.SecureString()
For Each character As Char In strPassword
SecuredPSWD.AppendChar(character)
Next
Dim wsmConnectionInfo As WSManConnectionInfo
Dim strSystemURI As String = "http://SERVER.DOMAIN/powershell?serializationLevel=Full"
Dim strShellURI As String = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"
Dim powerShellCredentials As PSCredential = New PSCredential(strUserName, SecuredPSWD)
wsmConnectionInfo = New WSManConnectionInfo(New Uri(strSystemURI), strShellURI, powerShellCredentials)
Dim runspace As Runspace = RunspaceFactory.CreateRunspace(wsmConnectionInfo)
Runspace.Open()
' runspace.RunspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", snapInException)
Dim pipeLine As Pipeline = runspace.CreatePipeline()
Dim command As Command = New Command("")
' TEST >> pipeLine.Commands.Add("Add-PSSnapin *Ex*")
Do While reader.Peek() <> -1
lineFromScript = Nothing
lineFromScript = reader.ReadLine()
pipeLine.Commands.Add(lineFromScript)
'command.Parameters.Add(lineFromScript)
'pipeLine.Commands.Add(command)
Loop
'' Run the contents of the pipeline
Dim psObjCollection As Collection(Of PSObject) = pipeLine.Invoke()
runspace.Close()
runspace.Dispose()
Return ""
End Function
I ended up working around the problem rather than fixing it.
I moved the script code into the vb.net application, and wrote each line to a file, i.e.
writer.WriteLine("Add-PSSnapin *Ex*")
Then I loaded the script through PowerShell as an application;
Dim exeStartInfo As System.Diagnostics.ProcessStartInfo
Dim exeStart As New System.Diagnostics.Process
exeStartInfo = New System.Diagnostics.ProcessStartInfo("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe")
exeStartInfo.Arguments = ("-command work\scriptbuilder.ps1")
exeStartInfo.WorkingDirectory = "C:\ExchangeManager\"
exeStartInfo.UseShellExecute = False
exeStart.StartInfo = exeStartInfo
exeStart.Start()
exeStart.Close()
Not ideal but it got the job done.

Text from webpage

I need to get some text from this web page. I want to use the trade feed for my program to analyse the sentiment of the markets.
I used the browser control and the get element command but its not working. The problem is that whenever my browser starts to open the page I get Java scripts errors.
I tried with DOM but seems that i dont quite understand what i need to do :)
Here is the code:
Dim code As String
Using client As New WebClient
code = client.DownloadString("http://openbook.etoro.com/ahanit/#/profile/Trades/")
End Using
Dim htmlDocument As IHTMLDocument2 = New HTMLDocument(code)
htmlDocument.write(htmlDocument)
Dim allElements As IHTMLElementCollection = htmlDocument.body.all
Dim allid As IHTMLElementCollection = allElements.tags("id")
Dim element As IHTMLElement
For Each element In allid
element.title = element.innerText
MsgBox(element.innerText)
Next
Update: So I tried the HTML Agility pack, as suggested in the comments, and I am stuck again on this code
Dim plain As String = String.Empty
Dim htmldoc As New HtmlAgilityPack.HtmlDocument
htmldoc.LoadHtml("http://openbook.etoro.com/ahanit/#/profile/Trades/")
Dim goodnods As HtmlAgilityPack.HtmlNodeCollection = htmldoc.DocumentNode.SelectNodes("THE PROBLEM")
For Each node In goodnods
TextBox1.Text = htmldoc.DocumentNode.InnerText
Next
Any advice what to now?
Ok I think I know what the problem is somehow the div that I need is hidden and its not loaded when I load the web page just the source code. Does someone knows how to load all the hidden divs ??
Here is my new code
Dim doc As New HtmlAgilityPack.HtmlDocument
Dim web As New HtmlWeb
doc = web.Load("http://openbook.etoro.com/ahanit/#/profile/Trades/")
Dim nodes As HtmlNode = doc.GetElementbyId("feed-items")
Dim id As String = nodes.WriteTo()
TextBox1.Text = TextBox1.Text & vbCrLf & id
user1336635,
Welcome to SO! Something you might try is to check out his source code, figure out what javascript function is populating the field you want (using firebug - I assume it's the one that "trades result in profit" next to it), and then embedding that script into a web page that your webbrowser control loads. That's where I'd try to start. I checked his source code and searched for "trades result in profit" and didn't find anything which leads me to believe hunting for the element 'might' not be possible. Just a starting place until someone with more experience with this chimes in!! Best!
-sf

HtmlAgilityPack - getting error when looping through nodes. Doesn't make sense

I'm trying to get all nodes below but I am getting an error message of:
Overload resolution failed because no accessible 'GetAttributeValue' accepts this number of arguments.
Dim doc As New HtmlDocument()
doc.LoadHtml("shaggybevo.com/board/register.php")
Dim docNode As HtmlNode = doc.DocumentNode
Dim nodes As HtmlNodeCollection = docNode.SelectNodes("//input")
For Each node As HtmlNode In nodes
Dim id As String = node.GetAttributeValue("id")
Next
Any ideas on why I am getting this error message? Thanks
You need to provide a default value as a second parameter to GetAttributeValue:
Dim id As String = node.GetAttributeValue("id", "")
Update for updated question
In addition to the above fix, you are retrieving the HtmlDocument incorrectly. HtmlDocument.Load will either load a file or an HTML string, not retrieve the file from the web server.
You need to modify your code to fetch the data from the URL using HtmlWeb. Replace the following lines:
Dim doc As New HtmlDocument()
doc.LoadHtml("shaggybevo.com/board/register.php")
with these:
Dim doc As HtmlDocument
Dim web As New HtmlWeb
doc = web.Load("http://shaggybevo.com/board/register.php")