• 通过服务端中转显示防盗链图片


     我有一个功能去采集别人网站有用的内容,采集内容中有一个图片链接,可以单独打开,但放在我的网站就无法显示,出错提示:

    很明显,有防盗链功能,只能在他的网站才能打开,这个好办,几行代码

    在我的网站加一个中转请求,在服务端获取图片流,输出到我的客户端(localhost):

    // GET: Home/PickUpImage?src=http://xxxx:yy
    public async Task<ActionResult> PickUpImage(string src)
    {
        if (string.IsNullOrEmpty(src)) return HttpNotFound();
        var stream = await HttpHelper.GetAsync(src);
        return File(stream, "image/png");
    }

    其中 HttpHelper 是我操作HTTP请求的工具类(必须是单例模式或静态方法类,不能做成实例类) ,里面是通过 HttpClient 实现的,每次请求都会记住Cookie(重点),部分代码实现:

    public class HttpHelper
        {
            private static readonly HttpClient _hc;
            static HttpHelper()
            {
                _hc = new HttpClient(new HttpClientHandler { AllowAutoRedirect = false, UseCookies = true });
                _hc.MaxResponseContentBufferSize = 2 * 1024 * 1024; //2MB
                _hc.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36");
                _hc.Timeout = new TimeSpan(0, 1, 0); //1 minute
            }
            public static async Task<Stream> GetAsync(string url)
            {
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
                    ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                }
                return await Client.GetStreamAsync(url);
            }
    }

    这样页面上img的src地址由

    https://cdn.whatismyipaddress.com/images/flags/mo.png

    改成

    http://localhost:3721/Home/PickUpImage?src=https://cdn.whatismyipaddress.com/images/flags/mo.png

    图片就能显示了

  • 相关阅读:
    discuz_ucenter_api_for_java的中文问题
    java的编码问题详解
    java的泛型的技巧
    向maven中央仓库提交jar
    Java并发之FutureTask
    java并发之Semaphore
    centos中JDK版本冲突的问题
    C++中嵌入Python
    boost::python的使用
    c++调用python引号的问题
  • 原文地址:https://www.cnblogs.com/felixnet/p/8972835.html
Copyright © 2020-2023  润新知