C # HttpListener "Richiesta non valida" problema
-
28-10-2019 - |
Domanda
Per qualche motivo, quando la richiesta viene inviata a HttpListener tramite un indirizzo non standard, restituisce
<h1>Bad Request (Invalid Hostname)</h1>
Pacchetto di esempio:
GET /index HTTP/1.1
Host: ::ffff:88.118.32.126:2548
Accept: */*
HTTP/1.1 400 Bad Request
Content-Type: text/html
Server: Microsoft-HTTPAPI/1.0
Date: Wed, 02 Feb 2011 19:05:18 GMT
Connection: close
Content-Length: 39
<h1>Bad Request (Invalid Hostname)</h1>
Come potrei risolvere questo problema?
Modifica: Questo è il mio codice
public class Server
{
private HttpListener Listener;
private Log Log;
public int Port = 1555;
public Server(Log _log)
{
Log = _log;
}
public void Start()
{
Listener = new HttpListener();
Listener.Prefixes.Add(string.Format("http://{0}:{1}/",
"88.118.32.126", Port));
Listener.Prefixes.Add(
string.Format("http://{0}:{1}/",
"*", Port)); //added these for testing, but still nothing
Listener.Prefixes.Add(
string.Format("http://::ffff:{0}:{1}/",
"*", Port)); //added these for testing, but still nothing
Listener.Start();
Listener.BeginGetContext(ProcessRequest, Listener);
Log.Add(string.Format("HTTP Server started on port {0}",
Port), LogType.Info, true);
}
private void ProcessRequest(IAsyncResult result)
{
HttpListener listener = (HttpListener)result.AsyncState;
HttpListenerContext context = listener.EndGetContext(result);
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
string[] UrlParts = request.RawUrl.Split('/');
Response output;
if (request.RawUrl == "/index")
output = new Response(context.Response.OutputStream,
ResponseType.Stats, UrlParts, Log);
response.ContentLength64 = output.Size;
response.ContentType = output.Header;
output.Send();
if (result.IsCompleted)
Log.Add(String.Format("OUT > {1,15} {0,8} bytes -> {2}",
output.Size, request.RemoteEndPoint.Address,
request.RawUrl), LogType.Info, true);
Listener.BeginGetContext(ProcessRequest, Listener);
}
}
Soluzione
Un modo in cui si verifica questo errore è quando HTTP.SYS non può abbinare l'intestazione dell'host HTTP con il suo elenco di UrlAcl validi. Ciò potrebbe essere dovuto a problemi nella risoluzione di un nome host.
- Risposta correlata che spiega i passaggi di configurazione
- Documentazione MSDN sulla configurazione di HTTP autonomo
- Correzione di una "richiesta errata (nome host non valido)" - Errore 400, su Windows IIS Server (potenziali soluzioni e commenti pertinenti)
Puoi elencare le mappature dalla riga di comando:
Strumenti di supporto di Windows XP / Strumenti di supporto 2003 - httpcfg
httpcfg query urlacl
Incluso con Windows Vista / 2008 / 7+ - netsh
netsh http show urlacl
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow