I have a function to collect useful content from other people's websites. There is a picture link in the collected content, which can be opened separately, but it cannot be displayed on my website. The error message is:
Obviously, there is anti-theft chain function, which can only be opened on his website. This is easy to handle, a few lines of code
Add a relay request to my website, get the image stream on the server, and output it to my client (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"); }
Among them, HttpHelper is my tool class for operating HTTP requests (it must be a singleton mode or a static method class, and cannot be made into an instance class), which is implemented through HttpClient, and remembers cookies (key points) for each request. Part of the code is implemented:
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); } }
In this way, the src address of img on the page is given by
https://cdn.whatismyipaddress.com/images/flags/mo.png
change to
http://localhost:3721/Home/PickUpImage?src=https://cdn.whatismyipaddress.com/images/flags/mo.png
The picture will show