} // Stop public void DisplayPrefixesAndState() { // List the prefixes to which the server listens: HttpListenerPrefixCollection prefixes = listener.Prefixes; if (prefixes.Count == 0) { DeviceServerApp.Logger.Error("There are no HttpListener prefixes."); return; } foreach (string prefix in prefixes) { DeviceServerApp.Logger.Information(prefix); } // Show the listening state: if (listener.IsListening) { DeviceServerApp.Logger.Information("The HttpListener is listening."); } else { DeviceServerApp.Logger.Warning("The HttpListener is not listening."); } } // DisplayPrefixesAndState
public void PropertiesWhenClosed2() { HttpListener listener = new HttpListener(); listener.Close(); HttpListenerPrefixCollection p = listener.Prefixes; }
public void Remove_DisposedListener_ThrowsObjectDisposedException() { var listener = new HttpListener(); HttpListenerPrefixCollection prefixes = listener.Prefixes; listener.Close(); Assert.Throws <ObjectDisposedException>(() => prefixes.Remove("http://localhost:9200/")); }
public void Clear_DisposedListener_ThrowsObjectDisposedException() { var listener = new HttpListener(); HttpListenerPrefixCollection prefixes = listener.Prefixes; listener.Close(); Assert.Throws <ObjectDisposedException>(() => prefixes.Clear()); }
// </snippet6> // <snippet7> public static string[] CopyPrefixes(HttpListener listener) { HttpListenerPrefixCollection prefixes = listener.Prefixes; string[] prefixArray = new string[prefixes.Count]; prefixes.CopyTo(prefixArray, 0); return(prefixArray); }
public HttpListener(ILogger logger) { _logger = logger; prefixes = new HttpListenerPrefixCollection(logger, this); registry = new Hashtable(); connections = Hashtable.Synchronized(new Hashtable()); auth_schemes = AuthenticationSchemes.Anonymous; }
ArrayList wait_queue; // List<ListenerAsyncResult> wait_queue; public HttpListener () { prefixes = new HttpListenerPrefixCollection (this); registry = new Hashtable (); ctx_queue = new ArrayList (); wait_queue = new ArrayList (); auth_schemes = AuthenticationSchemes.Anonymous; }
/// <summary> /// Initializes a new instance of the <see cref="HttpListener" /> class. /// </summary> /// <param name="certificate">The certificate.</param> public HttpListener(X509Certificate?certificate = null) { Certificate = certificate; _prefixes = new HttpListenerPrefixCollection(this); _connections = new ConcurrentDictionary <HttpConnection, object>(); _ctxQueue = new ConcurrentDictionary <string, HttpListenerContext>(); }
private void AddLocalIpAddresses(HttpListenerPrefixCollection prefixes) { var host = Dns.GetHostEntry(Dns.GetHostName()); foreach (var ip in host.AddressList.Where(ip => ip.AddressFamily == AddressFamily.InterNetwork)) { prefixes.Add($"http://{ip}:{_port}/"); } }
/// <summary> /// Initializes a new instance of the <see cref="HttpListener"/> class. /// </summary> public HttpListener () { _authSchemes = AuthenticationSchemes.Anonymous; _connections = new Dictionary<HttpConnection, HttpConnection> (); _contextQueue = new List<HttpListenerContext> (); _prefixes = new HttpListenerPrefixCollection (this); _registry = new Dictionary<HttpListenerContext, HttpListenerContext> (); _waitQueue = new List<ListenerAsyncResult> (); }
public HttpListener() { prefixes = new HttpListenerPrefixCollection(this); registry = new Hashtable(); connections = Hashtable.Synchronized(new Hashtable()); ctx_queue = new ArrayList(); wait_queue = new ArrayList(); auth_schemes = AuthenticationSchemes.Anonymous; }
/// <summary> /// Initializes a new instance of the <see cref="HttpListener"/> class. /// </summary> public HttpListener() { prefixes = new HttpListenerPrefixCollection (this); registry = new Dictionary<HttpListenerContext, HttpListenerContext> (); connections = new Dictionary<HttpConnection, HttpConnection> (); ctx_queue = new List<HttpListenerContext> (); wait_queue = new List<ListenerAsyncResult> (); auth_schemes = AuthenticationSchemes.Anonymous; }
public void CopyTo_DisposedListener_ThrowsObjectDisposedException() { var listener = new HttpListener(); HttpListenerPrefixCollection prefixes = listener.Prefixes; listener.Close(); Assert.Throws <ObjectDisposedException>(() => prefixes.CopyTo((Array) new string[1], 0)); Assert.Throws <ObjectDisposedException>(() => prefixes.CopyTo(new string[1], 0)); }
public HttpListener () { prefixes = new HttpListenerPrefixCollection (this); registry = new Hashtable (); connections = Hashtable.Synchronized (new Hashtable ()); ctx_queue = new ArrayList (); wait_queue = new ArrayList (); auth_schemes = AuthenticationSchemes.Anonymous; defaultServiceNames = new ServiceNameStore (); extendedProtectionPolicy = new ExtendedProtectionPolicy (PolicyEnforcement.Never); }
// </snippet3> // <snippet4> public static bool CheckForPrefix(HttpListener listener, string prefix) { // Get the prefixes that the Web server is listening to. HttpListenerPrefixCollection prefixes = listener.Prefixes; if (prefixes.Count == 0 || prefix == null) { return(false); } return(prefixes.Contains(prefix)); }
// </snippet4> // <snippet6> public static bool RemoveAllPrefixes(HttpListener listener) { // Get the prefixes that the Web server is listening to. HttpListenerPrefixCollection prefixes = listener.Prefixes; try { prefixes.Clear(); } // If the operation failed, return false. catch { return(false); } return(true); }
public static void SimpleListenerExample(HttpListenerPrefixCollection prefixes) { if (!HttpListener.IsSupported) { Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class."); return; } // URI prefixes are required, // for example "http://contoso.com:8080/index/". if (prefixes == null) { throw new ArgumentException("prefixes"); } // Create a listener. HttpListener listener = new HttpListener(); // Add the prefixes. foreach (string s in prefixes) { listener.Prefixes.Add(s); } listener.Start(); Console.WriteLine("Listening..."); // Note: The GetContext method blocks while waiting for a request. HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; // Obtain a response object. HttpListenerResponse response = context.Response; // Construct a response. string responseString = "<HTML><BODY> Hello world!</BODY></HTML>"; byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); // Get a response stream and write the response to it. response.ContentLength64 = buffer.Length; System.IO.Stream output = response.OutputStream; output.Write(buffer, 0, buffer.Length); // You must close the output stream. output.Close(); listener.Stop(); }
/// <summary> /// 打印监听地址信息测试 /// </summary> /// <param name="listener"></param> static void PrintPrefixes(HttpListener listener) { HttpListenerPrefixCollection prefixes = listener.Prefixes; if (prefixes.Count == 0) { Console.WriteLine("There are no prefixes."); } foreach (string prefix in prefixes) { Console.WriteLine("服务器已启动,监听:" + prefix); } // Show the listening state. if (listener.IsListening) { Console.WriteLine("The server is listening."); } }
public void Start(IList <string> prefixes) { lock (this.locker) { if (this.server != null) { throw new InvalidOperationException(); } this.server = new HttpListener(); HttpListenerPrefixCollection s = this.server.Prefixes; foreach (string prefixe in prefixes) { s.Add(prefixe); } this.server.Start(); this.InternalGetContextAsyncCycleLooper(null); } }
// <snippet1> public static void DisplayPrefixesAndState(HttpListener listener) { // List the prefixes to which the server listens. HttpListenerPrefixCollection prefixes = listener.Prefixes; if (prefixes.Count == 0) { Console.WriteLine("There are no prefixes."); } foreach (string prefix in prefixes) { Console.WriteLine(prefix); } // Show the listening state. if (listener.IsListening) { Console.WriteLine("The server is listening."); } }
/// <summary> /// Initializes a new instance of the DavContext class. /// </summary> /// <param name="listenerContext"><see cref="HttpListenerContext"/> instance.</param> /// <param name="prefixes">Http listener prefixes.</param> /// <param name="repositoryPath">Local path to repository.</param> /// <param name="logger"><see cref="ILogger"/> instance.</param> public DavContext( HttpListenerContext listenerContext, HttpListenerPrefixCollection prefixes, System.Security.Principal.IPrincipal principal, string repositoryPath, ILogger logger) : base(listenerContext, prefixes) { this.Logger = logger; this.RepositoryPath = repositoryPath; if (!Directory.Exists(repositoryPath)) { Logger.LogError("Repository path specified in Web.config is invalid.", null); } if (principal != null) { Identity = principal.Identity; } }
public void StartListening() { server.Start(); HttpListenerPrefixCollection prefixes = server.Prefixes; Manager.WriteLine($"Server started at: "); foreach (string prefix in prefixes) { Console.WriteLine($"\t{prefix}"); } new Thread(() => { while (true) { HttpListenerContext context = server.GetContext(); Thread thread = new Thread(new ParameterizedThreadStart(HandleRequest)); thread.Start(context); } }).Start(); }
private void displayPrefixesAndState() { HttpListenerPrefixCollection prefixes = m_listerner.Prefixes; if(prefixes.Count == 0) { SvLogger.Debug(" There are no prefixes."); } StringBuilder tempStr = new StringBuilder(); int index = 0; foreach (string prefix in prefixes) { tempStr.Clear(); tempStr.AppendFormat("Prefix {0} : {1}.", ++index, prefix); SvLogger.Debug(tempStr.ToString()); } if(m_listerner.IsListening) { SvLogger.Debug(" The Server Is Listening."); } }
/// <summary> /// Initializes a new instance of the DavContext class. /// </summary> /// <param name="context">Instance of <see cref="HttpListenerContext"/>.</param> /// <param name="prefixes">Collection of http listener prefixes.</param> /// <param name="user">Current use principal. null if anonymous.</param> public DavContext(HttpListenerContext context, HttpListenerPrefixCollection prefixes, IPrincipal user, ILogger logger) : base(context, prefixes) { this.currentUser = user; }
/// <summary> /// Initializes a new instance of the <see cref="HttpListener"/> class. /// </summary> public HttpListener() { _prefixes = new HttpListenerPrefixCollection(this); _registry = new Hashtable(); _connections = Hashtable.Synchronized(new Hashtable()); _ctxQueue = new ArrayList(); _waitQueue = new ArrayList(); #if AUTHENTICATION _authSchemes = AuthenticationSchemes.Anonymous; #endif //defaultServiceNames = new ServiceNameStore(); //_extendedProtectionPolicy = new ExtendedProtectionPolicy(PolicyEnforcement.Never); }
// </snippet1> public static void Main() { // <snippet2> // Set up a listener. HttpListener listener = new HttpListener(); HttpListenerPrefixCollection prefixes = listener.Prefixes; prefixes.Add(@"http://*****:*****@"http://contoso.com:8080/"); // Specify the authentication delegate. listener.AuthenticationSchemeSelectorDelegate = new AuthenticationSchemeSelector(AuthenticationSchemeForClient); // Start listening for requests and process them // synchronously. listener.Start(); // </snippet2> while (true) { // <snippet3> Console.WriteLine("Listening for {0} prefixes...", listener.Prefixes.Count); HttpListenerContext context = listener.GetContext(); HttpListenerRequest request = context.Request; Console.WriteLine("Received a request."); // This server requires a valid client certificate // for requests that are not sent from the local computer. // Did the client omit the certificate or send an invalid certificate? if (request.IsAuthenticated && request.GetClientCertificate() == null || request.ClientCertificateError != 0) { // Send a 403 response. HttpListenerResponse badCertificateResponse = context.Response; SendBadCertificateResponse(badCertificateResponse); Console.WriteLine("Client has invalid certificate."); continue; } // </snippet3> StringBuilder message = new StringBuilder(); // <snippet7> // When the client is not authenticated, there is no Identity. if (context.User == null) { message.Append("<HTML><BODY><p> Hello local user! </p></BODY></HTML>"); } else { // <snippet6> // Get the requester's identity. System.Security.Principal.WindowsIdentity identity = WindowsIdentity.GetCurrent(); // Construct the response body. message.AppendFormat("<HTML><BODY><p> Hello {0}!<br/>", identity.Name); message.AppendFormat("You were authenticated using {0}.</p>", identity.AuthenticationType); message.Append("</BODY></HTML>"); // </snippet6> } // Configure the response. HttpListenerResponse response = context.Response; // Use the encoding from the response if one has been set. // Otherwise, use UTF8. System.Text.Encoding encoding = response.ContentEncoding; if (encoding == null) { encoding = System.Text.Encoding.UTF8; response.ContentEncoding = encoding; } byte[] buffer = encoding.GetBytes(message.ToString()); response.ContentLength64 = buffer.Length; response.StatusCode = (int)HttpStatusCode.OK; response.StatusDescription = "OK"; response.ProtocolVersion = new Version("1.1"); // Don't keep the TCP connection alive; // We don't expect multiple requests from the same client. response.KeepAlive = false; // Write the response body. System.IO.Stream stream = response.OutputStream; stream.Write(buffer, 0, buffer.Length); // </snippet7> // <snippet4> Console.WriteLine("Request complete. Press q to quit, enter to continue."); string answer = Console.ReadLine(); if (answer.StartsWith("q")) { Console.WriteLine("bye."); listener.Close(); break; } // </snippet4> } }
/// <summary> /// Initializes a new instance of the <see cref="HttpListener"/> class. /// </summary> public HttpListener () { _authSchemes = AuthenticationSchemes.Anonymous; _connections = new Dictionary<HttpConnection, HttpConnection> (); _connectionsSync = ((ICollection) _connections).SyncRoot; _ctxQueue = new List<HttpListenerContext> (); _ctxQueueSync = ((ICollection) _ctxQueue).SyncRoot; _ctxRegistry = new Dictionary<HttpListenerContext, HttpListenerContext> (); _ctxRegistrySync = ((ICollection) _ctxRegistry).SyncRoot; _logger = new Logger (); _prefixes = new HttpListenerPrefixCollection (this); _waitQueue = new List<HttpListenerAsyncResult> (); _waitQueueSync = ((ICollection) _waitQueue).SyncRoot; }