Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi Guys,

 

How do i capture content from a browser based on a specific URL..

 

Lets say i navigate to google.com, how do i capture the whole content into a variable..

 

for some reasons im generating some xml file on server side, so i want

my users to actually access the data from the window application..

 

Appreciate if some guidelines/reference can be provided to get me started with this.

 

thanks

Posted

Do you mean something like this? Has nothing todo with a browser, but it should capture the contents. If you wished to grab the URL from a browser that should be possible also.

		string html;
		System.Net.WebClient wc;  
		System.IO.Stream myStream; 
		System.IO.StreamReader myReader;

		wc = new System.Net.WebClient();
		myStream = wc.OpenRead(@"http://www.google.com");
		html = myReader.ReadToEnd();

		myReader.Close();
		wc.Dispose();

 

By the way, this post shouldn't really be in the C# syntax section, as it isn't syntax specific. For future reference you should only post in this section if you have a syntax issue, not just because your useing C# :).

Anybody looking for a graduate programmer (Midlands, England)?
Posted

Oh ok, sorry for the posting in wrong section,

 

yea, thats the code i was looking for, thanks..

 

but i got a small error, that is

"use of unassigned local variable 'myReader'

 

Not sure what shall i assigned it to..Any idea?

 

thanks a lott

  • Leaders
Posted

you needed to declare myReader as new .

here's a way that uses a few less lines of code & less classes...

[size=2]System.Net.WebClient wClient = [/size][size=2][color=#0000ff]new[/color][/size][size=2] System.Net.WebClient();


[/size][size=2][color=#0000ff]byte[/color][/size][size=2][] buffer = wClient.DownloadData([url="http://www.google.co.uk"]http://www.google.co.uk[/url]);


[/size][size=2][color=#0000ff]string[/color][/size][size=2] html = System.Text.Encoding.Default.GetString(buffer, 0 , buffer.Length);


Console.WriteLine( html );

[/size]

Posted

i see, thanks a lott for all the help, is there any way to convert your code to read

the content of the webpage rather than the source code itself..thanks..

Posted
no i mean, the code above reads the source . meaning it includes the html tags and the data that is in the webpage, so what i plan to do was just to read the body content..meaning lets say this page, i just want to read what is see here, in this page, so i dont want to read the codes to build this page, but i want to read the values on this page (data)..hope i did not confuse you..
  • Administrators
Posted

That's what HTML is... A mixture of tags and content, there is no real concept of 'values' when dealing with HTML.

 

If you want to get to the text content then you will need to parse the tags to get at the content. If the HTML has a lot of content then you are probably going to have to invest some time in learning Regular Expressions as a way to parse out the tags etc.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...