locked
404 Image Handler RRS feed

  • Question

  • User-1025245042 posted

    Im wondering if anyone has any suggestions for the following scenario:

    An application we are working on "leeches" images from another web site (the other web site is also ours, we just dont need multiple copies our product catalog spread over every server).  Occasionally we point to an image that does not exist (html from "New Site" references "http://oldsite/product123.jpg). 

    Is there a way, using an httphandler or module (or any other way) to work around this?  For instance, could I point to http://NewSite/ImagePage.ashx?imagename=http://oldsite/product123.jpg and somehow determine if the image exists, and if so send it to the client and if not send a generic "not found" image?

    Thanks for any ideas or suggestions.

    Joe
     

    <hints id="hah_hints"></hints>
    Tuesday, August 14, 2007 1:24 PM

Answers

  • User-563238995 posted

    Maybe create a common web (virtual) to hold these images and each website could then point to a single common location. This was you can share images without the use of a handler. Just a thought.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 16, 2007 9:40 AM
  • User-1025245042 posted

    That's definately an idea worth trying, and something we have discussed implementing down the road.

    Here's what I ended up with for my handler (rough draft version)

     

    public class ImageLeechHandler : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "image/jpg";
                
                System.Net.WebClient client = new System.Net.WebClient();
                byte[] image = null;
                string imageURL = context.Request.QueryString[0];
    
                try
                {
                    image = client.DownloadData(imageURL);
                    context.Response.BinaryWrite(image);
                }
                catch (System.Net.WebException)
                {
                    image = client.DownloadData("http://url.to.an.image.that.says.404.sorry");
                    context.Response.BinaryWrite(image);
                }
                
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }

      And heres what I added to the config file to wire it together

      

    <httpHandlers>
          <add verb="*" path="image.handler" type="ProjectBase.Demo.ImageLeechHandler, ProjectBase.Demo"/>
    </httpHandlers>
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 16, 2007 10:52 PM

All replies

  • User-563238995 posted

    Maybe create a common web (virtual) to hold these images and each website could then point to a single common location. This was you can share images without the use of a handler. Just a thought.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 16, 2007 9:40 AM
  • User-1025245042 posted

    That's definately an idea worth trying, and something we have discussed implementing down the road.

    Here's what I ended up with for my handler (rough draft version)

     

    public class ImageLeechHandler : IHttpHandler
        {
    
            public void ProcessRequest(HttpContext context)
            {
                context.Response.ContentType = "image/jpg";
                
                System.Net.WebClient client = new System.Net.WebClient();
                byte[] image = null;
                string imageURL = context.Request.QueryString[0];
    
                try
                {
                    image = client.DownloadData(imageURL);
                    context.Response.BinaryWrite(image);
                }
                catch (System.Net.WebException)
                {
                    image = client.DownloadData("http://url.to.an.image.that.says.404.sorry");
                    context.Response.BinaryWrite(image);
                }
                
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }

      And heres what I added to the config file to wire it together

      

    <httpHandlers>
          <add verb="*" path="image.handler" type="ProjectBase.Demo.ImageLeechHandler, ProjectBase.Demo"/>
    </httpHandlers>
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 16, 2007 10:52 PM
  • User-563238995 posted

    Hmm how about extending this to:

     

    HttpRequest httpRequest = context.Request;
    string physicalPath = httpRequest.PhysicalPath;
    string imageType = string.Empty;
    string contentType = string.Empty;
    string requestUrl = httpRequest.Url.Host;
    string referrerUrl = httpRequest.UrlReferrer.Host;
    
    if (!string.IsNullOrEmpty(httpRequest.UrlReferrer.ToString()))
    	if (string.Compare(requestUrl, referrerUrl) != 0)
    		physicalPath = context.Server.MapPath("~/stop.gif");
    You could build off this by using a case statement for various image types then decide to server the image or not.
    Saturday, August 18, 2007 11:06 PM