How to find out which image was requested C #, ASP.Net

I am developing a web application. which will generate a random link pointing to an image on my server. something like - http://dummy.com/Images/Image1.jpg?id=19234

Here this link can be used by anyone on their site, now I just want to know how many sites are using my links and no one clicks on those links.

Can this be done with an HTTPModule?

0


source to share


4 answers


Is it as easy as Google? Search

link:http://dummy.com/Images/Image1.jpg?id=19234

      



If you want to do this programmatically, you will need to use the Google API.

+2


source


The problem with HttpHandler is that it usually only fires for requests processed by the ASP.Net engine - image requests are usually processed by IIS without going through the handler.

Your weblogs should be able to tell you who is abstracting for any given element on your servers - if you have them, and you have something to handle them, that will be more accurate than using Google.

Going ahead, one of the ways I've done this in the past is to create an image generated by HttpHandler (IHttpHandler implementation).

This will return the image as a stream (setting the content type to "image / jpeg") and you can add further processing (like the log where the request came from (referent) etc.).



The limitation I found with HttpHandler is that some services (like PBBS) require the image link to have an image extension. I got around this by handling all 404s with an ASP.Net page that validates the .jpg extension in the request. If it finds one, instead of returning a normal 404 page, it returns the required image. You need to configure a 404 handler in IIS, although the web.config error handler only runs for ASP.Net requests (web services and .aspx pages).

Handler example:

// Sample from the ASP.Net Personal Web Site Starter Kit
public class Handler : IHttpHandler
{
    public bool IsReusable
    {
        get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
        // Set up the response settings
        context.Response.ContentType = "image/jpeg";
        context.Response.Cache.SetCacheability(HttpCacheability.Public);
        context.Response.BufferOutput = false;

        // QueryString parameters are available here:
        // context.Request.QueryString["QueryStringKey"]

        // You can also access the Referrer object, and log the requests here.

        Stream stream;
        // Read your image into the stream, either from file system or DB
        if (stream == null)
        {
            stream = PhotoManager.GetPhoto();
        }

        // Write image stream to the response stream
        const int buffersize = 1024 * 16;
        var buffer = new byte[buffersize];
        int count = stream.Read(buffer, 0, buffersize);
        while (count > 0)
        {
            context.Response.OutputStream.Write(buffer, 0, count);
            count = stream.Read(buffer, 0, buffersize);
        }
    }
}

      

You might have similar code (or better yet, refactoring the main image stream into a generic class) in a 404 page that checks for an image extension and displays the image that way (again, setting the content type, etc.).

+1


source


Thinking too much is right. See http://code.google.com/intl/en/apis/ajaxsearch/documentation/#fonje_snippets or Google API. They give examples for PHP and Java, but there are also AJAX frameworks for ASP.NET ( http://www.asp.net/ajax/ ) and I'm pretty sure C # does as well.

0


source


You can change the extension of the image to extension aspx ( http://dummy.com/Images/Image1.aspx?id=19234 ) no problem with that because this page is the only thing that will do the Response.OutputStream of the image. That is, it will look like a jpg, but with that advantage you can have some other code to handle.

In this aspx (before serving the image) we will ask for the http_referer and it will be stored in the data table if this registry does not exist.

This is really useful if you want to restrict access to images. You can add some logic to disallow if they are not logged in.

0


source







All Articles