Connect url in c#

August 25th, 2010 by Juan Leave a reply »

Today you need to connect to web pages, either to extract information from a page or to connect to aWebService. DotNet or .Net provides us a tools to connect againt WebService, but there are excepcional cases, we need to connect in a more archaic and traditional, for example cURL library works.

In this case will show an example of how to connect to a URL of a WebService as fictitious and reflect their content and then be treated.

First create a String where to put the url of the link. Then contect objects using HttpWebRequest and HttpWebResponse and generate a StreamReader with the answer.

The code would look like:

  String baseUri = "http://rutaalwebserice";
  HttpWebRequest connection =
  (HttpWebRequest)HttpWebRequest.Create(baseUri);
 
  connection.Method = "GET";
  HttpWebResponse response =
  (HttpWebResponse)connection.GetResponse();
 
  StreamReader sr =
  new StreamReader(response.GetResponseStream(),
  Encoding.UTF8);

We have our variable sr (StreamReader) ara can use it to work with the assumption XML or HTML returned.

It is important to note, that to use these libraries, we need to include the following namespaces:

  using System.Web;
  using System.Net;
  using System.IO;
  using System.Text;

Via:  Diferencias entre preg_match y eregConectar a una url en C#

Advertisement

1 comment

  1. Awsome work boy.I realy like your blog.Keep up the good work

Leave a Reply