Search Forum
(53671 Postings)
Search Site/Articles

Archived Articles
712 Articles

C# Books
C# Consultants
What Is C#?
Download Compiler
Code Archive
Archived Articles
Advertise
Contribute
C# Jobs
Beginners Tutorial
C# Contractors
C# Consulting
Links
C# Manual
Contact Us
Legal

GoDiagram for .NET from Northwoods Software www.nwoods.com


 
Printable Version

Requesting a Web Page
By Efe Aras

Our developer team is working on multi technology software ( I mean macromedia flash, some services on a server with a slow internet connection, being written in C# and a web based software written with ASP). Now I am working on a service that sometimes need to communicate with the web server and get some data. I do not prefer to connect directly to the web server's database because we decided to leave the business logic on the web server. The other alternative was to use web services however why should I use a generic web service, to handle a business logic that will only used by my module. So we decided put a web page that will bring the list of users in XML format. All I would nee to do is connect to web server, pass my parameter in my GET request. It was the purchase order id I would pass and the list of users that are related to that Purchase Order would be passed back in XML format.

//sample purchase order id
string po_id=8;

//The address of the web page
string web_page_address="http://www.acmecompany.com/get_users.asp";


//the parameter is added to the end of the web page with a question mark
//then it will be a child game for that asp page to handle the request

string parameter="?purchase_order_id=" + po_id;
Now the main subject of our article; how could I make a HTTP GET request in C# or in .NET Framework. It was easier then I thought. The class that was designed for HTTP communication purposes was WebClient . As you can guess, it is located in System.Net.

//an instance of WebClient class
WebClient web_client = new WebClient();

//this function returns a byte array
Byte[] page_in_bytes = client.DownloadData(web_page_address + parameter );

//covert the byte array to string
string page_source = Encoding.ASCII.GetString(page_in_bytes);
Now it is ready for parsing. XML parsing is beyond our topic so I will not delve in to that topic. The main point while using DownloadData method is It is a blocking function (function that do not return until it finishes what it is doing) like many socket functions. This means your program will stall during DownloadData and If you have a slow Internet connection like me It is a long time. You should put this kind of blocking calls in Threads as a good engineering practice.