aspx页面图片用作html中img的url

背景:如果无法直接访问保存图片的服务器,我们可以先制作一个aspx页面用来接受服务器发送过来的图片,然后html页面请求aspx页面。对图片服务器起一定的缓冲保护作用,预防对黑客攻击造成危害。

 

注意:不可以是直接设置aspx页面中的图片控件的imageurl,此方法已实验不成功。

 

1. showpic.aspx页面

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="showpic.aspx.cs" Inherits="NewProject.showpic" %>

2. showpic.aspx.cs 后台代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.IO;
using System.Web.UI.WebControls;
using System.Net;

namespace NewProject
{
    public partial class showpic : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //Image.ImageUrl = "http://local/v1/tfs/T1.ysssTByYT1RCvBVdK";      
            string url = "http://local/v1/tfs/T1.ysefsefsefvBVdK";
            string contentType="application/x-www-form-urlencoded";
            Stream reqStream = SendGetRequestForStream(url,contentType);
            MemoryStream ms = new MemoryStream();
            reqStream.CopyTo(ms);
            //Response.Write(ms.ToArray());//这种方法错误
            Response.BinaryWrite(ms.ToArray());//可以
            //HttpContext.Current.Response.BinaryWrite(ms.ToArray());//可以
        }

        private Stream SendGetRequest1(string url)
        {
            string content;
            HttpRequest request;//     Enables ASP.NET to read the HTTP values sent by a client during a Web request.
            WebRequest webrequest;//     Makes a request to a Uniform Resource Identifier (URI). This is an abstract
            HttpWebRequest httpWebRequest;//     Provides an HTTP-specific implementation of the System.Net.WebRequest class.
            HttpWebResponse httpWebResponse;
            //string url = "fsefsf";
            httpWebRequest = WebRequest.Create(url) as HttpWebRequest;
            httpWebRequest.AllowAutoRedirect = true;
            httpWebRequest.Method = "GET";
            httpWebRequest.ContentType = "application/x-www-form-urlencoded";

            //httpwebrequest.ContentType = "application/json";
            //httpwebrequest.ContentType = "application/xml";
            //httpwebrequest.Headers.Add("url",url);

            httpWebResponse = httpWebRequest.GetResponse() as HttpWebResponse;
            Stream resStream = httpWebResponse.GetResponseStream();
            using (StreamReader sr = new StreamReader(resStream))
            {
                content = sr.ReadToEnd();
            }
            return resStream;
        }
}
}

3. pic.html 页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div>
        <img src="showpic.aspx" alt="Alternate Text" />
        
    </div>
</body>
</html>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。