以下代码简单实现HTTP,监听端口"http://192.168.0.147:8080/"等即监听本地。可在本地直接访问,然后根据访问传来的端口后面的指令来判断返回值。
```csharp
class Program
{
//static string url="http://localhost:8080/";
static string url = "http://*:8080/";
//static string url = "http://127.0.0.1:8080/";
static string urlThis = "http://192.168.0.147:8080/";
//static string urlThis = "http://*:8080/";
static void Main(string[] args)
{
//Console.WriteLine("Hello World!");
//Program a = new Program();
//a.Setup(8080);
//Console.Read();
sSocket = new HttpListener();
sSocket.Prefixes.Add(url);
sSocket.Start();
sSocket.BeginGetContext(new AsyncCallback(GetContextCallBack), sSocket);
Console.Read();
}
static HttpListener sSocket = null;
static void GetContextCallBack(IAsyncResult ar)
{
try
{
sSocket = ar.AsyncState as HttpListener;
HttpListenerContext context = sSocket.EndGetContext(ar);
sSocket.BeginGetContext(new AsyncCallback(GetContextCallBack), sSocket);
HttpListenerResponse response = context.Response;
Console.WriteLine(context.Request.Url.PathAndQuery);
//其它处理code
if (context.Request.Url.PathAndQuery == "/getDateTime")
{
//给客户端返回指定时间
string responseString = string.Format("<HTML><BODY> {0}</BODY></HTML>", DateTime.Now);
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
//对客户端输出相应信息.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
}
//返回文件 文本
if (context.Request.Url.PathAndQuery == "/getDateTimeText")
{
//给客户端返回指定时间文本
System.Threading.ThreadPool.QueueUserWorkItem(LoadFile.Method4, context);
}
//返回文件 图片
if (context.Request.Url.PathAndQuery == "/getBiaoqingPng")
{
//给客户端返回指定图片
System.Threading.ThreadPool.QueueUserWorkItem(LoadFile.Method5, context);
}
//返回网页 带图片
if (context.Request.Url.PathAndQuery == "/getHtmlBiaoqingPng")
{
Console.WriteLine("Hello World!");
//返回指定的网页带图片
string responseString = string.Format("<HTML><img src={0} alt =\"\"></HTML>", urlThis + "getBiaoqingPng");
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
//对客户端输出相应信息.
response.ContentLength64 = buffer.Length;
System.IO.Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
}
}
catch { }
}
HttpListener httpListener = new HttpListener();
public void Setup(int port = 8080)
{
httpListener.AuthenticationSchemes = AuthenticationSchemes.Anonymous;
httpListener.Prefixes.Add(string.Format("http://*:{0}/", port));//如果发送到8080 端口没有被处理,则这里全部受理,+是全部接收
httpListener.Start();//开启服务
Receive();//异步接收请求
}
private void Receive()
{
httpListener.BeginGetContext(new AsyncCallback(EndReceive), null);
}
private void EndReceive(IAsyncResult ar)
{
HttpListenerContext context = httpListener.EndGetContext(ar);
Dispather(context);//解析请求
Receive();
Console.WriteLine(context.Request.Url.PathAndQuery);
}
RequestHelper RequestHelper;
ResponseHelper ResponseHelper;
void Dispather(HttpListenerContext context)
{
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
RequestHelper = new RequestHelper(request);
ResponseHelper = new ResponseHelper(response);
RequestHelper.DispatchResources(fs => {
ResponseHelper.WriteToClient(fs);// 对相应的请求做出回应
});
}
}
代码写好之后,可以直接VS调试运行,但有些东西需要提前弄好。
防火墙相关,需要运行入网以及一些操作,可以直接选择关闭防火墙,一键最方便。
控制面板上面可以直接关闭防火墙。
然后就是管理员身份运行CMD。
以管理员CMD命令行执行:
①先删除可能存在的错误urlacl,这里的号代指localhost、127.0.0.1、192.168.199.X本地地址和+号等。
命令:netsh http delete urlacl url=http://:8080/
举例:netsh http delete urlacl url=http://+:8080/
②将上面删除的号地址重新加进url,user选择所有人
命令:netsh http add urlacl url=http://:8080/ user=Everyone
举例:netsh http add urlacl url=http://+:8080/ user=Everyone
③CMD配置防火墙
netsh advfirewall firewall Add rule name=“命令行Web访问8080” dir=in protocol=tcp localport=8080 action=allow
经过如上设置服务端就可以以httpListener.Prefixes.Add(“http://+:8080/”);监听地址开启监听。
客户端可以通过访问服务端8080端口。服务端本机也可以在浏览器中以localhost和127.0.0.1访问自身http服务器。
主要是netsh http add urlacl url=http://*:8080/ user=Everyone把这个运行之后,则可以在局域网电脑进行访问到自己的http,诸如返回文件,网页等都可以,然后具体的http搭建可以参考以下网址:
https://www.cnblogs.com/zhanhengzong/archive/2012/12/11/2813254.html
里边有很详细的http服务器内容,包括打开网页,下载文件,断点续传等等。
如此,基本上一个http服务器就完成了,可以在window系统的服务器上直接运行打包出来的exe,配置相关的部署,则可以通过公网IP等访问了。
269




被折叠的 条评论
为什么被折叠?



