如何在Http模块

时间:2017-11-23 06:13:59

标签: image httpmodule

我有两个网站(Web1和Web2)和一个域名(www.web1.com)。域映射到Web1。

我在Web1中注册了一个http模块。当请求到Web1并且Web1上存在Web页面时,它将显示Page。否则在Web1上找不到页面(404),则模块将向Web2发送请求并从Web2获取响应并写入显示。

Request Response flow diagram

这是我的http模块代码:

    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();

        }
    }
}

以下是我的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>

它适用于html标记。但无法使用相对路径渲染图像。如果我将图像src设置为绝对路径,那么它将正确呈现。

如果我在下面评论替换代码页面正在渲染但图像不是从Web2站点渲染(具有相对路径)。

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

问题:如何在相对路径的网页上呈现图像?   请帮帮我..

2 个答案:

答案 0 :(得分:0)

网站2的网页将是Website1的跨域请求。因此,您必须引用绝对路径才能从Website2渲染图像。如果你想引用相对路径,那些图像必须是Website1的一部分。

答案 1 :(得分:0)

我找到了解决方案。对于StreamReader实现的Images / PDF,我使用了BinaryReader和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);
        }            
    }