jump to navigation

How we developed a Proxy site (kinda Anonymizer) Part II.. July 6, 2009

Posted by boopalanj in .NET, ASP.NET.
Tags: , ,
trackback

Aarthi is yet another good resource from our team. [Aarthi is sitting besides me. Readers are requested to use their discretion while reading the previous sentence considering my situation while writing it ;-)].

You know what, she needed help in downloading a file which sized more than 5 MB [6.8 MB]. In our corporate, downloading is restricted to 5 MB  according to the text it shows [although it does not allow even 2 MB download sometimes. Such a mad filter!]. And she wanted to check out if our so-called Model anonymizer [in the second post of this blog] can download that and give it to her.

Normally, we tech-kiddos, obey an organization’s policy more than anything. But what Aarthi said was – she got impressed with our blog. Impressed? by our technical Blog? For us, it is a rare event. Forget Organization’s policies, and forget the organization too, if someone is going to be impressed with what we are doing. We don’t want to refuse that this site is kinda aladdin’s genie that would bring the file for her.

So, I sat to mofify the existing code – to download the files and throw it to the response, if url is given. There, I came to know, that, there is one more great class – WebClient - which allows downloading of file to a local folder from a Url. All these WebRequest, Webresponse and WebClient classes are available under System.Net namespace. The class WebClient also has a method – DownloadFile(string url, string filepath). What else is needed!

I quickly dragged a Label, TextBox and Button and in the OnClick event, I wrote the following code.

public void btnGo_OnClick(object sender, EventArgs e)
{
 string strURL = txtUrl.Text;

 if (strURL.Equals(String.Empty) || (!strURL.StartsWith(“www.”) && !strURL.StartsWith(“http:”) && !strURL.StartsWith(“https:”)))
 {
     return;
 }

 WebClient wbc = new WebClient();
 wbc.DownloadFile(strURL, Server.MapPath(“ImagesDyn/”+ strURL.Substring(strURL.LastIndexOf(“/”)+1)));

 FileStream fs = new FileStream(Server.MapPath(“ImagesDyn/” + strURL.Substring(strURL.LastIndexOf(“/”) + 1)), FileMode.Open);

 Byte[] btFile = new Byte[fs.Length];
 fs.Read(btFile, 0, (int)fs.Length);

 Response.AddHeader(“Content-Disposition”, “attachment;filename=” + strURL.Substring(strURL.LastIndexOf(“/”) + 1));
 Response.Buffer = true;
 Response.ContentType = “application/octet-stream”;
 Response.BinaryWrite(btFile);

 fs.Dispose();
 wbc.Dispose();

 File.Delete(Server.MapPath(“ImagesDyn/” + strURL.Substring(strURL.LastIndexOf(“/”) + 1)));
}

The file is downloaded to the local folder ‘ImageDyn‘  with the same file name. If I point to the Url of the Downloaded File using Response.Redirect(“”), it would get blocked through the download filter(although I did not check that. So, please don’t mind if it is wrong).  So I used FileStream, read the bytes into the byte array, and pushed it to the response. [I also delete the file at the end of process, but the hosting server did not allow to do this, so commented the last line.]

 The result – below: [It took hell lot of time before this dialog appeared. [Downloading to its local folder was slow]]

Downloading

Downloading

A sample implementation can be found at this Url: [Not valid after 90 days since it is a free host service, as said earlier.]
It can be used to download any Images, Documents, etc which are useful but blocked.
Also, For this to work, the code should be deployed in a hosting site (free/ paid) / or some server out of your network [Like we did.]
See ya!

Comments»

No comments yet — be the first.