在使用HttpListener 监听url时,如果没有以管理员身份运行可能会抛出异常,无法监听,遇到这个问题时,可以先使用管理员权限的程序将Url注册到urlacl列表,解决普通权限无法开启监听问题
第一步: Netsh http show urlacl。查看有没有需要监听的url
第二步:添加Url到Urlacl: 例如添加 "http://+:55474/"
/// <summary> /// 注册Url /// </summary> private void RegisterUrl() { var netshManger = new NetshManger(); string url = "http://+:55474/"; netshManger.AddAddress(url); }
/// <summary> /// Netsh管理 /// </summary> public class NetshManger { /// <summary> /// 添加url /// </summary> /// <param name="address"></param> public void AddAddress(string address) { try { AddAddress(address, Environment.UserDomainName, Environment.UserName); } catch (Exception ex) { Debug.Fail(ex.ToString()); } } public void AddAddress(string address, string domain, string user) { string argsDll = $@"http delete urlacl url={address}"; string args = $@"http add urlacl url={address} user=everyone"; //{domain}\{user}" ProcessStartInfo psi = new ProcessStartInfo("netsh", argsDll) { Verb = "runas", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false }; Process.Start(psi)?.WaitForExit();//删除urlacl psi = new ProcessStartInfo("netsh", args) { Verb = "runas", CreateNoWindow = true, WindowStyle = ProcessWindowStyle.Hidden, UseShellExecute = false }; Process.Start(psi)?.WaitForExit();//添加urlacl } }
标签:urlacl,http,string,url,HttpListener,address,监听 From: https://www.cnblogs.com/terryK/p/17443210.html