Page 1 of 1

Web intigration?

Posted: Thu Jul 24, 2008 12:59 am
by DEEhunter
How would I make my C# application receive values from a web page and store them as variables. I'm not sure if it will help but I already have the div classes that I want to receive integers from.

Re: Web intigration?

Posted: Thu Jul 24, 2008 8:24 am
by LuxuriousMeat
DEEhunter wrote:How would I make my C# application receive values from a web page and store them as variables. I'm not sure if it will help but I already have the div classes that I want to receive integers
from.
Here is a very basic example.

Code: Select all

using System.Net;

//send a request to the page and get the response back
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("url");
HttpWebResponse res = (HttpWebresponse)req.GetResponse();

//store the whole page output in a string
string page = new StreamReader(res.GetResponseStream()).ReadToEnd();

//get the inner text of a certain div tag
string[] div = page.Split(new string[] { "<div>" }, StringSplitOptions.None);
string InnerText = div[1].Split(new string[] { "</div>" }, StringSplitOptions.None)[0];

MessageBox.Show(InnerText);

Posted: Thu Jul 24, 2008 12:19 pm
by DEEhunter
I continue to get a "Index was outside the bounds of the array." error.

Posted: Thu Jul 24, 2008 1:14 pm
by Altimit01
How many elements are in the string array "div"? It'd probably be a good idea to put a check making sure that the array has elements. I know that code wouldn't work on this page though. Searching for just "<div>" can cause problems when they are more like "<div align..."

Posted: Thu Jul 24, 2008 3:10 pm
by DEEhunter
I used div ids in it actually. But to make it more specific I tried to use <span> instead using a span ID.

Posted: Thu Jul 24, 2008 7:23 pm
by LuxuriousMeat
Altimit01 wrote:How many elements are in the string array "div"? It'd probably be a good idea to put a check making sure that the array has elements. I know that code wouldn't work on this page though. Searching for just "<div>" can cause problems when they are more like "<div align..."
I know it won't completely work for him, I gave an example of how to do it and he needs to modify it for it to work for him.

Posted: Thu Jul 24, 2008 10:51 pm
by Altimit01
Obviously. I was just pointing out that if it was used unmodified it'd cause problems.

Anyways the best thing is to do a check before accessing the elements to make sure there are elements to access. Ubound, length or whatever the language appropriate method is. Highly recommend going through this in a debugger.