Click here to Skip to main content
16,015,351 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to develop an application in the proxy server, which is capable of caching the web pages, which i would use later to cache pages which i would have browsed more times.

So i want a code in C# for that. if anybody could help me for that.

OR what are the preferred steps (algorithms) to do that?

Best regards...
Posted
Updated 7-Nov-11 9:54am
v2
Comments
Mostafa8 30-Nov-11 4:25am    
I've an exception here:-
Unhandled Exception: System.Net.HttpListenerException: Access is denied
at System.Net.HttpListener.Start()

1 solution

Here's a starting point, this link has some proxy server code.

http://code.cheesydesign.com/?p=393[^]

Then what you need is an algorithm to change the URL to a filename, which you can store in your cache. So some code like this:

C#
protected static string UrlToFilename(string url) {
  // With jpegs and height fields the bits after the ? seem to vary from machine to machine

  string filename = url;

  // Truncate the filename and add a hash if it is too long
  if (filename.Length > 160) {
    MD5 hash = MD5.Create();
    byte[] hexResult = hash.ComputeHash(Encoding.Unicode.GetBytes(url));
    filename = filename.Substring(0, 160) + BitConverter.ToString(hexResult).Replace("-", "");
  }

  int index;
  while ((index = filename.IndexOfAny(System.IO.Path.GetInvalidFileNameChars())) != -1) {
    filename = filename.Replace(filename[index], '_');
  }
  return filename;
}


Then from here you can store the files in a directory structure of your choosing, and in the proxy server, check if the file exists, if so use the file, else fetch from the internet.
 
Share this answer
 
Comments
Mostafa8 25-Nov-11 16:04pm    
Thank you very much Mr.John, but I've 2 questions.

first:-

I can't under stand this function:-
static void RespCallback(IAsyncResult asynchronousResult)
{
try
{

// State of request is asynchronous.
RequestData requestData = (RequestData)asynchronousResult.AsyncState;
Console.WriteLine("Got back response from " + requestData.Context.Request.Url.AbsoluteUri);

using(HttpWebResponse response = (HttpWebResponse)requestData.WebRequest.EndGetResponse(asynchronousResult))
using (Stream receiveStream = response.GetResponseStream())
{
HttpListenerResponse responseOut = requestData.Context.Response;

// Need to get the length of the response before it can be forwarded on
responseOut.ContentLength64 = response.ContentLength;
int bytesCopied = CopyStream(receiveStream, responseOut.OutputStream);
responseOut.OutputStream.Close();
Console.WriteLine("Copied {0} bytes", bytesCopied);
}
}
catch (WebException e)
{......



Second:-
I'm developing the application on my computer not at a company has a proxy server...-this is a graduation project - so how can I debug the program using a proxy server especially its cache?

Thank you again,
Best regards...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900