gpt4 book ai didi

c# - Web 服务器 HttpListener 响应

转载 作者:可可西里 更新时间:2023-11-01 17:10:27 27 4
gpt4 key购买 nike

我在以正确方式显示 HttpListenerResponse 时遇到了一些问题。我正在使用 http://www.codehosting.net/blog/BlogEngine/post/Simple-C-Web-Server.aspx作为样本!我想显示“1.html”,但我不能!当我在浏览器中显示它时,它显示得很好。当我尝试显示为响应时,它显示没有图像 girl.png

"1.html"代码

    <HTML>
<BODY>
My web page.<br>
<p>
<img src="girl.png" width="189" height="255" alt="lorem">
<input id="btnLogin" type="button" value="login" " style="width:100px;" />
</p>
</BODY>
</HTML>

WebServer.cs

using System;
using System.Net;
using System.Threading;
using System.Linq;
using System.Text;
using System.IO;

namespace SimpleWebServer
{
public class WebServer
{
private readonly HttpListener _listener = new HttpListener();
private readonly Func<HttpListenerRequest, string> _responderMethod;

public WebServer(string[] prefixes, Func<HttpListenerRequest, string> method)
{
if (!HttpListener.IsSupported)
throw new NotSupportedException(
"Needs Windows XP SP2, Server 2003 or later.");

// URI prefixes are required, for example
// "http://localhost:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");

// A responder method is required
if (method == null)
throw new ArgumentException("method");

foreach (string s in prefixes)
_listener.Prefixes.Add(s);

_responderMethod = method;
_listener.Start();
_listener.BeginGetContext(new AsyncCallback(ListenerCallback), _listener);
}
private static void ListenerCallback(IAsyncResult result)
{
HttpListener listener = (HttpListener)result.AsyncState;
listener.BeginGetContext(new AsyncCallback(ListenerCallback), listener);
Console.WriteLine("New request.");

HttpListenerContext context = listener.EndGetContext(result);
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;


byte[] page = GetFile("1.html");

response.ContentLength64 = page.Length;
Stream output = response.OutputStream;
output.Write(page, 0, page.Length);
output.Close();


}
public static byte[] GetFile(string file)
{
if (!File.Exists(file)) return null;
FileStream readIn = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[1024 * 1000];
int nRead = readIn.Read(buffer, 0, 10240);
int total = 0;
while (nRead > 0)
{
total += nRead;
nRead = readIn.Read(buffer, total, 10240);
}
readIn.Close();
byte[] maxresponse_complete = new byte[total];
System.Buffer.BlockCopy(buffer, 0, maxresponse_complete, 0, total);
return maxresponse_complete;
}

public WebServer(Func<HttpListenerRequest, string> method, params string[] prefixes)
: this(prefixes, method) { }

public void Run()
{
ThreadPool.QueueUserWorkItem((o) =>
{
Console.WriteLine("Webserver running...");
try
{
while (_listener.IsListening)
{
ThreadPool.QueueUserWorkItem((c) =>
{
var ctx = c as HttpListenerContext;
try
{
string rstr = _responderMethod(ctx.Request);
byte[] buf = Encoding.UTF8.GetBytes(rstr);
ctx.Response.ContentLength64 = buf.Length;
ctx.Response.OutputStream.Write(buf, 0, buf.Length);
}
catch { } // suppress any exceptions
finally
{
// always close the stream
ctx.Response.OutputStream.Close();
}
}, _listener.GetContext());
}
}
catch { } // suppress any exceptions
});
}
public void Stop()
{
_listener.Stop();
_listener.Close();
}
} }

主窗体中的代码

private void Form1_Load(object sender, EventArgs e)
{
WebServer ws = new WebServer(SendResponse, "http://localhost:8080/test/");
ws.Run();
Console.WriteLine("A simple webserver. Press a key to quit.");
Console.ReadKey();
ws.Stop();
}
public static string SendResponse(HttpListenerRequest request)
{
return string.Format("<HTML><BODY>My web page.<br>{0} <p><input type='submit' value='Отправить'></BODY></HTML>", DateTime.Now); }

如何在浏览器中显示我的 1.html?

最佳答案

当我查看您的代码时,我首先看到的是您的图像是通过相对路径引用的。如果您将 HTML 代码流式传输到另一台服务器,如果该图像不在另一端的相对位置,它就无法找到该图像。

使用图像的完整 URL 引用(例如 http://otherserver.com/girl.png),它应该可以访问。

关于c# - Web 服务器 HttpListener 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20787051/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com