开发者

Route HTTP images for a HTTPS site

I'm running a site where users can link images and thumbnails from other sites in their content. When viewing these images in the https secured user are, they are getting securit开发者_C百科y warnings, because http content is contained in the https page..

To work around this, I'd like to route the images through my server to the client, there by "giving" them the required https protokoll.

e.g. when viewing content on the secure site an image tag would like this: <img src="https://mysite/img.aspx?src=http://url.to/someimage.jpg" >

As my site using Umbraco (.NET 3.5, IIS7), I've already looked into using the urlrewritingnet library, but it only seems to be able to rewrite and redirect urls.

Has anybody done this?


The following works quite well:

I've got it to work by just passsing through the image bytes on the server. I'm not entierly convinced that it is a good solution so, I'll wait for better solutions:

public partial class Img : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            string url = Page.Request.QueryString["url"];

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
            request.Timeout = 5000;
            request.ReadWriteTimeout = 20000;
            HttpWebResponse imgresponse = (HttpWebResponse)request.GetResponse();
            //StreamReader sr = new StreamReader(imgresponse.GetResponseStream());


            Response.ContentType = "image/gif";
            byte[] fileBytes = GetFileBytes(imgresponse.GetResponseStream());
            Response.BinaryWrite(fileBytes);
            Response.Flush();

        }

        protected byte[] GetFileBytes(Stream stream)
        {

            byte[] fileBytes = null;
            byte[] buffer = new byte[4096];

            try
            {

                MemoryStream memoryStream = new MemoryStream();
                int chunkSize = 0;
                do
                {
                    chunkSize = stream.Read(buffer, 0, buffer.Length);
                    memoryStream.Write(buffer, 0, chunkSize);
                } while (chunkSize != 0);
                fileBytes = memoryStream.ToArray();

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }

            return fileBytes;
        }

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜