![]() |
|
|
How to read web pages/services in code |
|
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. |
|
|
|
| Sponsored Links (We share ad revenue): |
|
|
Re: How to read web pages/services in code |
|
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.
|
|
|
|
|
|
Re: How to read web pages/services in code |
|
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 |
|
|
|
|
|
Re: How to read web pages/services in code |
|
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?
|
|
|
|
|
|
Re: How to read web pages/services in code |
|
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 |
|
|
|
|
|
Re: How to read web pages/services in code |
|
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.
|
|
|
|
|
|
Re: How to read web pages/services in code |
|
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 |
|
|
|
|
|
Re: How to read web pages/services in code |
|
Moderator
![]()
Latest Blog Post:
My Favorite Isaac Asimov Story Posts: 4,070
Name: John Alexander
|
You rock, Chris!
|
|
|
|
|
|
Re: How to read web pages/services in code |
|
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 |
|
|
|
| Sponsored Links (We share ad revenue): |
| Thread Tools | |
|
|
| Webmaster Resources Marketplace: |
| Software Development Company | Webhosting.UK.com |
| Web Templates | Text Link Brokers | Stock Photos |