Requesting a Web Page


Our developer team is working on multitechnology software ( I mean macromedia flash, some services on aserver with a slow internet connection, being written in C# and a webbased software written with ASP). Now I am working on a service thatsometimes need to communicate with the web server and get some data. Ido not prefer to connect directly to the web server's database becausewe decided to leave the business logic on the web server. The otheralternative was to use web services however why should I use a genericweb service, to handle a business logic that will only used by mymodule. So we decided put a web page that will bring the list of usersin XML format. All I would nee to do is connect to web server, pass myparameter in my GET request. It was the purchase order id I would passand the list of users that are related to that Purchase Order would bepassed 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 couldI make a HTTP GET request in C# or in .NET Framework. It was easierthen I thought. The class that was designed for HTTP communicationpurposes 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 isbeyond our topic so I will not delve in to that topic. The main pointwhile using DownloadData method is It is a blocking function (functionthat do not return until it finishes what it is doing) like many socketfunctions. This means your program will stall during DownloadData andIf you have a slow Internet connection like me It is a long time. Youshould put this kind of blocking calls in Threads as a good engineeringpractice. <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="468" height="60"><param name="movie" value="/banners/Ad2.swf?clickTAG=http://www.red-gate.com/products/ants_profiler/index.htm?utm_source=chelp%26utm_medium=banner%26utm_content=vsmenu%26utm_campaign=antsprofiler" /><param name="quality" value="high" /> <embed src="http://www.csharphelp.com/banners/Ad2.swf?clickTAG=http://www.red-gate.com/products/ants_profiler/index.htm?utm_source=chelp%26utm_medium=banner%26utm_content=vsmenu%26utm_campaign=antsprofiler" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="468" height="60"></embed> </object>

Twitter Digg Delicious Stumbleupon Technorati Facebook Email

No comments yet... Be the first to leave a reply!