-
Dec 22nd, 2010, 01:23 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] httpwebresponse and getelmentbyid
I have been used getelmentbyid in past in vb6 using webbrowser control and internetexplorer instance
But in vb.net i tried to fetch webpge using the httpwebresponse Which gives the html / source in the form of sting
Is it possible to fetching data by getelementbyid using httpwebresponse and httpwebrequest method ?
Thanks
Last edited by CatchItBaby; Dec 22nd, 2010 at 08:10 PM.
-
Dec 22nd, 2010, 01:31 PM
#2
Re: httpwebresponse and getelmentbyid
Hello,
getElementById is a JavaScript function, so you will not be able to execute that on the string variable that you get back.
However, what you could do is use the HTML Agility Pack:
http://htmlagilitypack.codeplex.com/
To help you parse the returned HTML.
Gary
-
Dec 22nd, 2010, 01:31 PM
#3
Re: httpwebresponse and getelmentbyid
I don't believe so. The httpwebrequest returns a String with all the HTML source, which you then have to parse yourself. If you look in my sig, I just posted a Class (Scraper) that makes fetching HTML elements a bit easier.
-
Dec 22nd, 2010, 03:07 PM
#4
Re: httpwebresponse and getelmentbyid
Depending on what you are doing, you could use the webbrowser control, as the Document property has a getElementById method.
-
Dec 22nd, 2010, 03:11 PM
#5
Re: httpwebresponse and getelmentbyid
you can write to the mshtml.IHtmlDocument2 if you add a reference to Microsoft HTML object library, like this...
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://www.google.co.uk"), HttpWebRequest)
Dim resp As HttpWebResponse = DirectCast(req.GetResponse, HttpWebResponse)
Dim sr As New IO.StreamReader(resp.GetResponseStream)
'/// Add a Reference to the Microsoft HTML Library (Project... Add Reference...COM.
Dim doc As New mshtml.HTMLDocument
Dim iDocument As mshtml.IHTMLDocument2 = doc
iDocument.open()
iDocument.write(sr.ReadToEnd)
iDocument.close()
'/// test a valid ElementID that's in the webpage to see if it's worked...
MessageBox.Show(doc.getElementById("gbi").innerHTML)
sr.Close()
resp.Close()
End Sub
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Dec 22nd, 2010, 03:21 PM
#6
Re: httpwebresponse and getelmentbyid
Originally Posted by dynamic_sysop
you can write to the mshtml.IHtmlDocument2 if you add a reference to Microsoft HTML object library, like this...
Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim req As HttpWebRequest = DirectCast(HttpWebRequest.Create("http://www.google.co.uk"), HttpWebRequest)
Dim resp As HttpWebResponse = DirectCast(req.GetResponse, HttpWebResponse)
Dim sr As New IO.StreamReader(resp.GetResponseStream)
'/// Add a Reference to the Microsoft HTML Library (Project... Add Reference...COM.
Dim doc As New mshtml.HTMLDocument
Dim iDocument As mshtml.IHTMLDocument2 = doc
iDocument.open()
iDocument.write(sr.ReadToEnd)
iDocument.close()
'/// test a valid ElementID that's in the webpage to see if it's worked...
MessageBox.Show(doc.getElementById("gbi").innerHTML)
sr.Close()
resp.Close()
End Sub
Oh wow! that's awesome. I didn't know you could do that. Does it wait until the entire page is loaded to get all the document? Is there a "newer" implementation instead of the COM object or is that it?
-
Dec 22nd, 2010, 04:00 PM
#7
Re: httpwebresponse and getelmentbyid
you cant create a New HtmlDocument using the System.Net version ( as far as i'm aware ) hence using mshtml to write the doc.
it basically put's the fully loaded stream of the streamreader directly into the document that you've created, then you can grab any element as you would when using a browser (but without actually using a browser)
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Dec 22nd, 2010, 09:38 PM
#8
Thread Starter
Hyperactive Member
Re: httpwebresponse and getelmentbyid
Thanks Perfectly Work
-
Dec 23rd, 2010, 02:32 AM
#9
Re: httpwebresponse and getelmentbyid
Originally Posted by dynamic_sysop
you can write to the mshtml.IHtmlDocument2 if you add a reference to Microsoft HTML object library, like this...
I agree that it is good to know that you can do this, but creating this document for the sole purpose of doing a getElementById seems like a little bit of overkill to me. You can equally as well find it using a Regular Expression, or the HTML Agility Pack that I mentioned before.
Gary
-
Dec 24th, 2010, 11:06 AM
#10
Thread Starter
Hyperactive Member
Re: [RESOLVED] httpwebresponse and getelmentbyid
I tried but now i m getting this error
Last edited by CatchItBaby; Dec 27th, 2010 at 03:30 AM.
-
Dec 24th, 2010, 11:13 AM
#11
Re: [RESOLVED] httpwebresponse and getelmentbyid
Is it the .write line that is throwing the exception?
Gary
-
Dec 24th, 2010, 11:29 AM
#12
Thread Starter
Hyperactive Member
Re: [RESOLVED] httpwebresponse and getelmentbyid
it didn't display error in the coding line but a new popup box arise
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|