Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# C中图像的Web服务器响应#_C#_Html_Uwp - Fatal编程技术网

C# C中图像的Web服务器响应#

C# C中图像的Web服务器响应#,c#,html,uwp,C#,Html,Uwp,我正在尝试用C#as UWP开发一个小程序,它能够以HTML页面的形式通过http发送响应。实际上,我可以用以下代码用文本流回答: using (var output = args.Socket.OutputStream) { using (var response = output.AsStreamForWrite()) { var html = Encoding.UTF8.GetBytes(htmlResponse); using (var

我正在尝试用C#as UWP开发一个小程序,它能够以HTML页面的形式通过http发送响应。实际上,我可以用以下代码用文本流回答:

using (var output = args.Socket.OutputStream)
 {
    using (var response = output.AsStreamForWrite())
    {
        var html = Encoding.UTF8.GetBytes(htmlResponse);
        using (var bodyStream = new MemoryStream(html))
        {
            var header = $"HTTP/1.1 200 OK\r\nContent-Length: {bodyStream.Length}\r\nConnection: close\r\n\r\n";
            var headerArray = Encoding.UTF8.GetBytes(header);
            await response.WriteAsync(headerArray, 0, headerArray.Length);
            await bodyStream.CopyToAsync(response);
            await response.FlushAsync();
        }
    }
 }
答案是

HTTP/1.1 200 OK
Content-Length: {bodyStream.Length}
Connection: close
但当我尝试用jpeg图像或png图像回答时,浏览器不会解释答案。我试图转换字节数组,流,Base64中的图像,但什么也做不到。 我该怎么做


Thanx很多

我认为您只需要添加一个带有mime类型的
标题
image/jpeg
,并尝试使用上面列出的类型进行响应。(字节数组、流、Base64)

您需要设置
响应。ContentType=“image/jpeg”
响应。ContentType=“image/png”
。否则浏览器不知道如何渲染图像