I am trying to create a proxy, text based requests get processed(js, css, html) properly. But when i try to load in an image i get the message that the image contains errors.
I have tried multiple ways to send this over http but none of them have worked yet. Here is the relevant code
try
{
StringBuilder responseString = new StringBuilder();
HttpWebRequest clientRequest = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse response = (HttpWebResponse)clientRequest.GetResponse();
string headers = response.Headers.ToString();
string contentType = response.Headers.Get("content-type");
var encoding = ASCIIEncoding.ASCII;
if (contentType.IndexOf("image") > -1)
{
using (BinaryReader stream = new BinaryReader(response.GetResponseStream()))
{
var bytes = new byte[1024];
while (true)
{
var n = stream.Read(bytes, 0, bytes.Length);
if (n == 0)
{
break;
}
responseString.Append(bytes);
}
return "HTTP/1.1 200 \n" + headers + responseString.ToString();
}
}
else
{
using (var reader = new System.IO.StreamReader(response.GetResponseStream(), encoding))
{
return "HTTP/1.1 200 \n" + headers + reader.ReadToEnd();
}
}
catch
{
addToList("Request failed");
return "Request failed";
}
}
If possible i would prefer to keep the http response string so that I send it over the already existing NetworkStream.
The solution was to stream the response directly to the client and not convert it to a string first. The headers are sent in a string before.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.