Search code examples
imagehttpmodule

How to render image/pdf in Http Module


I have two website (Web1 and Web2) and one domain (www.web1.com). Domain is mapped to Web1.

I have one http Module registered in Web1. When Request comes to Web1 and web page exist on Web1, it will display the Page. Otherwise page not found (404) on Web1, then module will send request to Web2 and get the response from Web2 and write to display.

Request Response flow diagram

Here is my http Module code:

    public class Testmodule : IHttpModule
{
    public void Dispose()
    {
        //clean-up code here.
    }
    public void Init(HttpApplication context)
    {
        context.PreSendRequestContent += context_PreSendRequestContent;
    }
    private void context_PreSendRequestContent(object sender, EventArgs e)
    {
        HttpContext context = HttpContext.Current;
        if (context.Response.StatusCode == 404)//On 404 on Web1 it will pass the the condition.
        {
            string strSite = WebConfigurationManager.AppSettings["Web2SiteURL"];
            string requestURL = ((HttpApplication)sender).Request.Url.ToString();
            string sourceURL = requestURL.Replace("http://" + HttpContext.Current.Request.Url.Authority, strSite);

            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(sourceURL);
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

            string WebResp_html = string.Empty;
            using (StreamReader sr = new StreamReader(WebResp.GetResponseStream()))
            {
                WebResp_html = sr.ReadToEnd();
            }
            string strCurrentUrl = "http://" + HttpContext.Current.Request.Url.Authority + "/";

            //Replacing the image src Relative URL to Absolute URL in the html source code.
            //If I comment below Replacement code Page is rendering but images are not rendering from Web2 site.
            //Actually  the image/pdf/swg files from html source code with relative path not rendering properly.
            WebResp_html = WebResp_html.Replace(strSite, strCurrentUrl);

            context.Response.Write(WebResp_html);
            context.Response.Flush();
            context.Response.Close();

        }
    }
}

Below is code of my code for Web2 Page2

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        This is my Page 2 on Web2.
        <br/>
    <p><img width="312" height="312" class="images" alt="" src="Images/Img1.jpg"/></p>
    </div>
    </form>
</body>
</html>

It works fine for html tag. But could not render images with relative path. If I make the image src to absolute path then it render properly.

If I comment below Replacement code Page is rendering but images are not rendering from Web2 site (with relative path).

WebResp_html = WebResp_html.Replace(strSite, strCurrentUrl); //converting image src from relative to absolute path.

Question : How can I render image on web page with relative path? Please help me..


Solution

  • I found solution. For Images/PDF instated of StreamReader I have used BinaryReader and BinaryWrite

        private void RenderBinaryContent(HttpWebResponse response)
        {
            using (var br = new BinaryReader(response.GetResponseStream()))
            {
                byte[] tempBytes;
                byte[] imageBytes;
    
                using (MemoryStream ms = new MemoryStream())
                {
                    tempBytes = br.ReadBytes(4096);
                    while (tempBytes.Length > 0)
                    {
                        ms.Write(tempBytes, 0, tempBytes.Length);
                        tempBytes = br.ReadBytes(1024);
                    }
                    imageBytes = new byte[(int)ms.Length];
                    ms.Position = 0;
                    ms.Read(imageBytes, 0, imageBytes.Length);
                }
    
                HttpContext.Current.Response.BinaryWrite(imageBytes);
            }            
        }