C#HttpListener’错误请求’问题

前端之家收集整理的这篇文章主要介绍了C#HttpListener’错误请求’问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
出于某种原因,当请求通过非标准地址发送到HttpListener时,它返回
  1. <h1>Bad Request (Invalid Hostname)</h1>

示例包:

  1. GET /index HTTP/1.1
  2. Host: ::ffff:88.118.32.126:2548
  3. Accept: */*
  4.  
  5. HTTP/1.1 400 Bad Request
  6. Content-Type: text/html
  7. Server: Microsoft-HTTPAPI/1.0
  8. Date: Wed,02 Feb 2011 19:05:18 GMT
  9. Connection: close
  10. Content-Length: 39
  11.  
  12. <h1>Bad Request (Invalid Hostname)</h1>

我怎么能解决这个问题?

编辑:
这是我的代码

  1. public class Server
  2. {
  3. private HttpListener Listener;
  4. private Log Log;
  5.  
  6. public int Port = 1555;
  7.  
  8. public Server(Log _log)
  9. {
  10. Log = _log;
  11. }
  12.  
  13. public void Start()
  14. {
  15. Listener = new HttpListener();
  16. Listener.Prefixes.Add(string.Format("http://{0}:{1}/","88.118.32.126",Port));
  17.  
  18. Listener.Prefixes.Add(
  19. string.Format("http://{0}:{1}/","*",Port)); //added these for testing,but still nothing
  20.  
  21. Listener.Prefixes.Add(
  22. string.Format("http://::ffff:{0}:{1}/",but still nothing
  23.  
  24. Listener.Start();
  25. Listener.BeginGetContext(ProcessRequest,Listener);
  26.  
  27. Log.Add(string.Format("HTTP Server started on port {0}",Port),LogType.Info,true);
  28. }
  29.  
  30. private void ProcessRequest(IAsyncResult result)
  31. {
  32. HttpListener listener = (HttpListener)result.AsyncState;
  33. HttpListenerContext context = listener.EndGetContext(result);
  34.  
  35. HttpListenerRequest request = context.Request;
  36. HttpListenerResponse response = context.Response;
  37.  
  38. string[] UrlParts = request.RawUrl.Split('/');
  39. Response output;
  40.  
  41. if (request.RawUrl == "/index")
  42. output = new Response(context.Response.OutputStream,ResponseType.Stats,UrlParts,Log);
  43.  
  44. response.ContentLength64 = output.Size;
  45. response.ContentType = output.Header;
  46.  
  47. output.Send();
  48.  
  49. if (result.IsCompleted)
  50. Log.Add(String.Format("OUT > {1,15} {0,8} bytes -> {2}",output.Size,request.RemoteEndPoint.Address,request.RawUrl),true);
  51.  
  52. Listener.BeginGetContext(ProcessRequest,Listener);
  53. }
  54. }

解决方法

发生此错误的一种方法是HTTP.SYS无法将HTTP Host标头与其有效UrlAcl列表匹配.这可能是因为解析主机名时出现问题.

> Related answer explaining configuration steps
> MSDN documentation about setting up HTTP stand-alone
> Fixing a “Bad Request (Invalid Hostname)” – 400 Error,on Windows IIS Server(相关潜在解决方案和评论)

您可以在命令行列出映射:

Windows XP Support Tools/2003 Support Tools – httpcfg

  1. httpcfg query urlacl

包含在Windows Vista / 2008/7中 – netsh

  1. netsh http show urlacl

猜你在找的C&C++相关文章