How to get full URL from WebBrowser? - vb.net

Hey guys I am using a webrower to get an access token to a website. The website redirects the webrower to a url containing the access token the redirected url looks like this:
http://www.myurl.us/#access_token=e0a81edc8de4886b7cc514bcb2b93e06666bd0d8&expires_in=3600&token_type=bearer&refresh_token=72e302438349b32a627b12c92b01060169443a9b&account_username=
in the DocumentCompleted event for my webrowser I am using this code:
Dim pretoken As String
Dim url As String = WebBrowser1.Url.ToString
If url.Contains("myurl.us") Then
pretoken = WebBrowser1.Url.ToString
MessageBox.Show(pretoken)
End If
The message box only shows "myurl.us" and not the full url with the token i need. Anyway to get the whole url from the webbrowser?

Have you tried Url.AbsoluteUri.ToString(), like:
Dim url As String = WebBrowser1.Url.AbsoluteUri.ToString()
Be careful with AbsoluteUri it's not AbsoluteUrl

Dim sPagePath As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath

Related

Download source of URL as string

I want my code to download the source/html of a website and be stored as a string.
For example
Dim source as string = html of website
This should work
Dim thesource As String = New System.Net.WebClient().DownloadString("http://the site u wanna look#")

Specific page to open in webbrowser

To open a specific page on vb.net we can use code
Dim webAddress As String = "http://www.example.com/"
Process.Start(webAddress)
Refference : Open a webpage in default webbrowser
But what if we wan't to open the page like localhost/thing ??
Just make sure you use http:// in front of your localhost url:
Dim webAddress As String = "http://localhost/thing"
Process.Start(webAddress)

How can I use decode encoded URL using VB.NET

I have an encoded url as follows;
dim inStr As String ="https%3A%2F%2Fmyweb%2page1%2Egov%2Emy%3A448%2Fserver%2FForm%3Fuserid%3Dtrouble"
I want to decode the url and assign this url to my webBrowser control.
From this thread:
Dim decodedUrl As String = HttpUtility.UrlDecode(encodedUrl)
I got a snippet to decode but am not using web application, am using windows application; can anyone suggest me the best way to do this?
you can use Uri.UnescapeDataString() method as follows:
Dim inputURL As String = "https%3A%2F%2Fmypage%2Eme%2Epage%2Esample%3A448%2Flfserver%2FForm%3Fuserid%3DTrouble"
Dim outputURL As String = Uri.UnescapeDataString(inputURL)
The output will be https://mypage.me.page.sample:448/lfserver/Form?userid=Trouble

.ToString Error (vb.net)

For some reason, this is not working for me , and I'm not sure. I'm trying to make a Set Home Page button for my webbrowser, and this is the code to check if a page is set, and then goto the page:
Dim HomepageInfo As String
If IO.File.Exists(Environment.SpecialFolder.ApplicationData & "\Homepage.Info") = True Then
HomepageInfo = IO.File.ReadAllText(Environment.SpecialFolder.ApplicationData & "\Homepage.Info")
WebBrowser1.Url = HomepageInfo.ToString
Else
'Create a File with a Default Homepage (www.google.com)
IO.File.WriteAllText(Environment.SpecialFolder.ApplicationData & "\Homepage.Info", "www.google.com")
End If
And this is showing as an error: HomepageInfo.ToString, and the error is: "Value of type 'String' cannot be converted to 'System.Uri'."
Thanks for any help!
WebBrowser.Url property accepts an uri object not a string:
Property Value Type: System.Uri A Uri representing the URL of the
current document.
So you have to use an instance of Uri class:
Provides an object representation of a uniform resource identifier
(URI) and easy access to the parts of the URI.
Code:
Dim HomepageInfo As String
If IO.File.Exists(Environment.SpecialFolder.ApplicationData & "\Homepage.Info") = True Then
HomepageInfo = IO.File.ReadAllText(Environment.SpecialFolder.ApplicationData & "\Homepage.Info")
WebBrowser1.Url = New Uri(HomepageInfo.ToString)
Else
'Create a File with a Default Homepage (www.google.com)
IO.File.WriteAllText(Environment.SpecialFolder.ApplicationData & "\Homepage.Info", "www.google.com")
End If
Quite simple: Your WebBrowser1.Url is not a string, but an URL - an URL is NOT a string, but behaves idfferently, e.g. options for checking he validity of an URL.
You can construct a new URL to circumvent this problem:
WebBrowser1.Url = new Uri(HomepageInfo.ToString)
But this might fail, of the given string is not a valid URL.
Try have to use like this
WebBrowser1.Url = new Uri(HomepageInfo.ToString);

vb.net problems posting to web form

I've hit a wall on POST'ing data to a webform :(
Below is the code I've adapted from many places (here most recently: http://p2p.wrox.com/asp-net-1-0-1-1-professional/34517-webrequest-webresponse-form-submit-problem-help.html )
Dim url As String = "https://student.ashford.edu/student/"
Dim data As String = "username=myusername&password=mypassword"
Dim buffer As Byte() = Encoding.UTF8.GetBytes(data)
Dim result As String = ""
Dim req As HttpWebRequest = DirectCast(WebRequest.Create(url), HttpWebRequest)
req.Method = "POST"
req.ContentType = "application/x-www-form-urlencoded"
req.ContentLength = buffer.Length
req.CookieContainer = New CookieContainer()
' enable cookies
Dim reqst As Stream = req.GetRequestStream()
' add form data to request stream
reqst.Write(buffer, 0, buffer.Length)
reqst.Flush()
reqst.Close()
Console.WriteLine(vbLf & "Posting data to " & url)
Dim res As HttpWebResponse = DirectCast(req.GetResponse(), HttpWebResponse)
' send request,get response
Console.WriteLine(vbLf & "Response stream is: " & vbLf)
Dim resst As Stream = res.GetResponseStream()
' display HTTP response
Dim sr2 As New StreamReader(resst)
result = sr2.ReadToEnd()
Using writer As System.IO.StreamWriter = New StreamWriter("C:\Temp\checkcheck.html")
writer.Write(result)
End Using
When I check the checkcheck.html file, it doesn't show the page I normally see when I've successfully logged into my university, but instead shows the login page again prompting for a user name and password. Since it's prompting for a user/pass in the output response, does that mean nothing is posted? Or is the login button not being hit?
As far as the field names in the source, the name of the username field is "username", and the name of the password field is "password". From what I can tell, the name of the login button itself is "submit". Below is the source of the page, am I missing something?
view-source:https://student.ashford.edu/student/
https://student.ashford.edu/student/
What I want to have happen is that in my VB.NET backend, I click a button and my username and password are automatically entered and I am automagically logged in. Once the login is done, I want to retrieve the HTML of the logged in page so I can parse it for items relating to my studies. This may be unrelated, but do I need cookies to keep my application "logged in" if I navigate to other pages?
EDIT:
I tried plugging different URL's, to no avail. I have also tried adding submit=login and login=submit (alternately of course) to the POST data. I'm still getting the original login screen HTML from the result variable at the end, instead of my university homepage that I'm expecting after I normally login by hand as a human would. Maybe there is a follow up step that I am missing?
The actual form action is at https://student.ashford.edu/login/commands/login.php. You might plug that into url instead. Also, try including the submit button - submit=login - in the values.
I found out what I needed to do. First I have to make sure that I have a cookie container. Next, I used Fiddler to find out EXACTLY what is being sent back and forth between my browser and where I want to login. I post all the login info, and I get the response back. This seems to fill up my cookie container variable with the required authentication cookies? Next I make another POST request to the page I want to go to, and voila - it works.