Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process URL and Get data

I have a URL which returns array of data. For example if I have the URL http://test.com?id=1 it will return values like 3,5,6,7 etc…

Is there any way that I can process this URL and get the returned values without going to browser (to process the url within the application)?

Thanks

like image 949
huMpty duMpty Avatar asked Jan 15 '13 14:01

huMpty duMpty


2 Answers

Really easy:

using System.Net;

...

var response = new WebClient().DownloadString("http://test.com?id=1");
like image 182
Dave Bish Avatar answered Oct 11 '22 08:10

Dave Bish


string urlAddress = "YOUR URL";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlAddress);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
  Stream receiveStream = response.GetResponseStream();
  StreamReader readStream = null;
  if (response.CharacterSet == null)
    readStream = new StreamReader(receiveStream);
  else
    readStream = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
  string data = readStream.ReadToEnd();
  response.Close();
  readStream.Close();
}

This should do the trick.

It returns the html source code of the homepage and fills it into the string data.

You can use that string now.

Source: http://www.codeproject.com/Questions/204778/Get-HTML-code-from-a-website-C

like image 45
jAC Avatar answered Oct 11 '22 08:10

jAC