I'm using a HTTPListener to receive requests and I'm currently trying to replace all the image tags from the response with a placeholder image before writing to ResponseStream. Unfortunately the image doesn't get displayed but instead I see the alt message, even though the image source is correct. I even used the "copy full path" option from Visual Studio to make sure it is (and then changed all the backslashes for forward slashes).
when I copy the image source and open it in a new tab it will open the correct image, so the image src is definitely correct
this is how I filter my HTML string and replace it with the placeholder:
private string filterContentPlaceholder(string html) {
Regex imgFilter = new Regex(@"</?img((\s+\w+(\s*=\s*(?:"".*?""|\'.*?\'|[^\'"">\s]+))?)+\s*|\s*)/?>");
string placeholderSrc = @"file://C:/Users/THE_Full_path";
string result = imgFilter.Replace(html, $"<img src={placeholderSrc} alt='Placeholder image'/>");
return result;
}
I then use this code to write to the stream (in which Output is the string from the previous function):
private void WriteToResponseStream(string Output, HttpListenerResponse Response) {
try {
byte[] bOutput = System.Text.Encoding.UTF8.GetBytes(Output);
Response.ContentType = "text/html";
Response.ContentLength64 = bOutput.Length;
Stream OutputStream = Response.OutputStream;
OutputStream.Write(bOutput, 0, bOutput.Length);
OutputStream.Close();
} catch (Exception e) {
MessageBox.Show(e.Message);
}
}
When I load a website the image doens't get shown but the alt-text does and the source shows up correct in the element inspector. Why isn't it showing the image?
Aucun commentaire:
Enregistrer un commentaire