HttpListener
1、以管理员权限打开CMD命令行
2、输入 netsh http show urlacl查看http://192.168.1.11:8081/在不在里面
3、先删除可能存在的错误urlacl,这里的*号代指localhost、127.0.0.1、192.168.199.X本地地址和+号等。
命令:netsh http delete urlacl url=http://*:8081/
这边使用: netsh http delete urlacl url=http://192.168.1.11:8081/
4、将上面删除的地址重新加进url,user选择所有人
命令:netsh http add urlacl url=http://*:8080/ user=Everyone
这边使用:netsh http add urlacl url=http://192.168.1.11:8081/ user=Everyone
5、配置防火墙
可以用下面命令来配置
netsh advfirewall firewall Add rule name="命令行Web访问8081" dir=in protocol=tcp localport=8081 action=allow
或者通过防火墙界面的 入站规则 里面添加一个8081的TCP端口
private void StartServer() { sSocket = new HttpListener(); //sSocket.Prefixes.Add("http://127.0.0.1:8889/zmq/"); sSocket.Prefixes.Add("http://192.168.124.11:8889/zmq/"); sSocket.Start(); sSocket.BeginGetContext(new AsyncCallback(GetContextCallBack), sSocket); } void GetContextCallBack(IAsyncResult ar) { try { if (!isRuning) { return; } sSocket = ar.AsyncState as HttpListener; HttpListenerContext context = sSocket.EndGetContext(ar); sSocket.BeginGetContext(new AsyncCallback(GetContextCallBack), sSocket); var request = context.Request; var response = context.Response; response.StatusCode = 200; response.ContentType = "application/json; charset=utf-8"; response.ContentEncoding = Encoding.UTF8; response.AppendHeader("Content-Type", "application/json;charset=UTF-8"); using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8)) { writer.Write(Newtonsoft.Json.JsonConvert.SerializeObject(zmqmessage)); zmqmessage.ttt.Clear(); writer.Close(); response.Close(); } } catch { } }