C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Taking Asynchronous cache of web pages using Web Streams of C#


Posted Date: 27 Mar 2007    Resource Type: Code Snippets    Category: Web Access

Posted By: Prajnan Das       Member Level: Gold
Rating:     Points: 10



.NET provides Web Streams support to read web contents. Using this feature, you can just as easily read from any web page on the Internet rather than reading from a stream provided by a custom server. WebRequest and WebResponse are the two core classes related to Web Streams. A WebRequest is an object that requests a Uniform Resource Identifier (URI) such as the URL for a web page. You can use a WebRequest object to create a WebResponse object that will encapsulate the object pointed to by the URI. That is, you can call GetResponse( ) on your WebRequest object to get the actual object (e.g., a web page) pointed to by the URI. What you get back is encapsulated in a WebResponse object. You can then ask that WebResponse object for a Stream object by calling GetResponseStream(). GetResponseStream( ) returns a stream that encapsulates the contents of the web object (e.g., a stream with the web page).

This example presents a class WebCache which retrieves the contents of a web page (example taken is the home page of The Time of India) as a stream and writes the content to a local disc file. Note that it is bulit on Asynchronous mechanism; so that in your code you can start the cache and proceed with other task. This would be helpful to take cache of large web pages or when the network is slow.

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;

public class WebCache
{
AsyncCallback ResponseCallBack;
AsyncCallback ReadCallBack;
HttpWebRequest request;
HttpWebResponse response;
static int bufferSize = 1024;
byte[] bytes = new byte[bufferSize];
Stream stream;
FileStream fileStream;

public void AsyncCache(string uri, string cacheName)
{
fileStream = File.Open(cacheName, FileMode.Create, FileAccess.Write, FileShare.None);
ResponseCallBack = new AsyncCallback(OnResponse);
//The Create method of WebRequest is overloaded on the type of the parameter.
//It returns different derived types depending on what is passed in. Here as
//you have passed in a URI, an object of type HTTPWebRequest is created. The
//return type, however,is WebRequest, and so you must cast the returned value
//to HTTPWebRequest.
request = (HttpWebRequest)WebRequest.Create(uri);
request.BeginGetResponse(ResponseCallBack, null);
}

void OnResponse(IAsyncResult ar)
{
if (ar.IsCompleted)
{
response = (HttpWebResponse)request.EndGetResponse(ar);
stream = response.GetResponseStream();
ReadCallBack = new AsyncCallback(OnRead);
stream.BeginRead(bytes, 0, bytes.Length, ReadCallBack, null);
}
}

void OnRead(IAsyncResult ar)
{
int bytesRead = stream.EndRead(ar);
if (bytesRead > 0)
{
fileStream.Write(bytes, 0, bytesRead);
stream.BeginRead(bytes, 0, bytes.Length, ReadCallBack, null);
}
else
{
Console.WriteLine("Caching Completed!");
stream.Close();
fileStream.Close();
response.Close();
}
}
}

Here is a test code:

new WebCache().AsyncCache("http://timesofindia.indiatimes.com/", @"D:\cache.html");




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Hyperlink to MSN Online Chat - MSNIM: commands
Previous Resource: Checking URL's - go to second url if first url fails using axWebBrowser
Return to Discussion Resource Index
Post New Resource
Category: Web Access


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

masks masks masks

Contact Us    Privacy Policy    Terms Of Use