huMpty duMpty
huMpty duMpty

Reputation: 14460

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

Upvotes: 7

Views: 30571

Answers (3)

Dave Bish
Dave Bish

Reputation: 19646

Really easy:

using System.Net;

...

var response = new WebClient().DownloadString("http://test.com?id=1");

Upvotes: 28

xfx
xfx

Reputation: 1358

This is a simple function I constantly use for similar purposes (VB.NET):

Public Shared Function GetWebData(url As String) As String
    Try
        Dim request As WebRequest = WebRequest.Create(url)
        Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
        Dim dataStream As Stream = response.GetResponseStream()
        Dim readStream As StreamReader = New StreamReader(dataStream)
        Dim data = readStream.ReadToEnd()
        readStream.Close()
        dataStream.Close()
        response.Close()
        Return data
    Catch ex As Exception
        Return ""
    End Try
End Function

To use it, pass it the URL and it will return the contents of the URL.

Upvotes: 2

jAC
jAC

Reputation: 5324

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

Upvotes: 2

Related Questions