Reply
How to read web pages/services in code
Old 02-20-2007, 06:28 PM How to read web pages/services in code
Learning Newbie's Avatar
Moderator

Latest Blog Post:
My Favorite Isaac Asimov Story
Posts: 4,070
Name: John Alexander
string url = "http://domain.com/pageOrService.ashx?qryString=" + someVariable, allData;
WebClient w = new WebClient();
StreamReader sr = new StreamReader(w.OpenRead(url));
allData = sr.ReadToEnd();
sr.Dispose();


From here you can use the MS XML DOM if the html is well formed, or you can use string manipulation, or even a 3rd party component. But depending on what you might want to do with an upstream web service or page ( probably a page unless someone forgot to set up their WSDL ), it could be pretty easy. For example, maybe you just need to know whether the page has a title:

if(!allData.Contains("<title>"))
//Tell the user to get with it

Even if this is an over-simple example, it shows you how to get data by making HTTP GET calls, how to stuff the document that gets sent down into a string variable, and from that, you can do anything with it.
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Sponsored Links (We share ad revenue):
 
Old 02-20-2007, 06:30 PM Re: How to read web pages/services in code
Learning Newbie's Avatar
Moderator

Latest Blog Post:
My Favorite Isaac Asimov Story
Posts: 4,070
Name: John Alexander
Oh, man, I shouldn't have stayed up so late last night, but when friends come over, I can't turn them away. Anyway, I forgot to mention that anyone can use this without having to ask first. I came up with this by going through all of Microsoft's documentation online, and grabbed bits and pieces until it worked. Anybody else would be able to do the same thing, I'm just hoping this will save someone a bit of time.
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Old 02-21-2007, 04:59 PM Re: How to read web pages/services in code
chrishirst's Avatar
Super Moderator

Posts: 10,639
Location: Blackpool. UK
should this be in the .net forum ?
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-21-2007, 05:06 PM Re: How to read web pages/services in code
Learning Newbie's Avatar
Moderator

Latest Blog Post:
My Favorite Isaac Asimov Story
Posts: 4,070
Name: John Alexander
Maybe it should. Is the .NET forum specifically for ASP.NET? I kind of got the impression anything ASP related goes here and the other forum was for people making desktop applications, although maybe that doesn't make sense on a webmaster site, huh?
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Old 02-21-2007, 05:25 PM Re: How to read web pages/services in code
chrishirst's Avatar
Super Moderator

Posts: 10,639
Location: Blackpool. UK
Yep

In Theory This is "classic" ASP for us dinosaurs who still can't see any real benefit in .net
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-21-2007, 06:48 PM Re: How to read web pages/services in code
Learning Newbie's Avatar
Moderator

Latest Blog Post:
My Favorite Isaac Asimov Story
Posts: 4,070
Name: John Alexander
It all makes sense now! Sorry for posting this in the wrong place, and you can go ahead and ignore the other thread recommending people to use int.TryParse instead of int.Parse; I posted that before I understood the difference between these two forums.
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Old 02-22-2007, 02:53 AM Re: How to read web pages/services in code
chrishirst's Avatar
Super Moderator

Posts: 10,639
Location: Blackpool. UK
No Problem

it's flagged to be moved.
__________________
Chris. ->> Links are advertising NOT optimising!! <<-
Indifference will be the downfall of mankind, but who cares?
Code Samples | People Counting System
chrishirst is online now
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 02-22-2007, 02:56 PM Re: How to read web pages/services in code
Learning Newbie's Avatar
Moderator

Latest Blog Post:
My Favorite Isaac Asimov Story
Posts: 4,070
Name: John Alexander
You rock, Chris!
Learning Newbie is online now
Reply With Quote
View Public Profile
 
Old 02-15-2008, 09:50 PM Re: How to read web pages/services in code
ozzozo's Avatar
Experienced Talker

Latest Blog Post:
Customer Support Whitepaper
Posts: 45
Very nice post. I have been working on a search engine / spider.

once you have the webpage into your string... you can:

Get the meta tags:

Code:
        private void GetMeta(string strIn)
        {
            // --- Grab the <TITLE> ---
            Match TitleMatch = Regex.Match(strIn, "<title>([^<]*)</title>", RegexOptions.IgnoreCase | RegexOptions.Multiline );
            title = TitleMatch.Groups[1].Value;
            
            // --- Parse out META KEYWORDS data ---
            Match KeywordMatch = Regex.Match( strIn, "<meta name=\"keywords\" content=\"([^<]*)\">", RegexOptions.IgnoreCase | RegexOptions.Multiline );
            keywords = KeywordMatch.Groups[1].Value;
            
            // --- Parse out META DESCRIPTION data ---
            Match DescriptionMatch = Regex.Match( strIn, "<meta name=\"description\" content=\"([^<]*)\">", RegexOptions.IgnoreCase | RegexOptions.Multiline );
            description = DescriptionMatch.Groups[1].Value;
        }
__________________
Universal FAQ Manager - FAQ Management Software
ozzozo is offline
Reply With Quote
View Public Profile Visit ozzozo's homepage!
 
Sponsored Links (We share ad revenue):
 
Reply     « Reply to How to read web pages/services in code
 

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML


Page generated in 0.20614 seconds with 14 queries