2015-01-04 10 views
4

웹 서버로 작동하도록 작은 C# 콘솔 앱이 있습니다. 같은 네트워크에있는 장치로 NAT에 잘 응답하지만 외부 IP에서 브라우저로 액세스하려고하면 400이됩니다.C#의 간단한 웹 서버를 사용하는 400 잘못된 요청 (잘못된 호스트)

라우터가 포트 포워드로 구성되어 있습니다. 그렇지 않으면 404가 표시됩니다.

로컬 호스트 : 8888/테스트가 정상적으로 작동합니다. 192.168.0.x : 8888/모든 장치에 대한 테스트.

xxx.xxx.xxx.xxx:8888/test가 HTTP 오류 400과 함께 실패합니다. 요청 호스트 이름이 유효하지 않습니다.

제안 사항?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Threading.Tasks; 

namespace httpsrv 
    { 
    class Program 
     { 
     static void Main(string[] args) 
      { 
      WebServer ws = new WebServer(SendResponse, "http://localhost:8888/test/"); 
      ws.Run(); 
      Console.WriteLine("Pi server started"); 
      Console.ReadKey(); 
      ws.Stop(); 
      } 

     public static string SendResponse(HttpListenerRequest request) 
      { 
      return string.Format("<HTML><BODY>Hosted from rasp. pi!<br>{0}</BODY></HTML>", DateTime.Now); 
      } 
     } 
    } 

웹 서버 클래스 :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Net; 
using System.Text; 
using System.Threading; 
using System.Threading.Tasks; 

namespace httpsrv 
    { 
    public class WebServer 
     { 
     private readonly HttpListener _listener = new HttpListener(); 
     private readonly Func<HttpListenerRequest, string> _responderMethod; 

     public WebServer(string[] prefixes, Func<HttpListenerRequest, string> method) 
      { 
      if (!HttpListener.IsSupported) 
       throw new NotSupportedException(
        "Needs Windows XP SP2, Server 2003 or later."); 

      if (prefixes == null || prefixes.Length == 0) 
       throw new ArgumentException("prefixes"); 

      if (method == null) 
       throw new ArgumentException("method"); 

      foreach (string s in prefixes) 
       _listener.Prefixes.Add(s); 

      _responderMethod = method; 
      _listener.Start(); 
      } 

     public WebServer(Func<HttpListenerRequest, string> method, params string[] prefixes) 
      : this(prefixes, method) { } 

     public void Run() 
      { 
      ThreadPool.QueueUserWorkItem((o) => 
      { 
       Console.WriteLine("Webserver running..."); 
       try 
        { 
        while (_listener.IsListening) 
         { 
         ThreadPool.QueueUserWorkItem((c) => 
         { 
          var ctx = c as HttpListenerContext; 
          try 
           { 
           string rstr = _responderMethod(ctx.Request); 
           byte[] buf = Encoding.UTF8.GetBytes(rstr); 
           ctx.Response.ContentLength64 = buf.Length; 
           ctx.Response.OutputStream.Write(buf, 0, buf.Length); 
           } 
          catch { } 
          finally 
           { 
           ctx.Response.OutputStream.Close(); 
           } 
         }, _listener.GetContext()); 
         } 
        } 
       catch { } 
      }); 
      } 

     public void Stop() 
      { 
      _listener.Stop(); 
      _listener.Close(); 
      } 
     } 
    } 

답변

1
  1. 어느 당신의 DNS 이름이나 해상도가 불량입니다.
  2. 당신이 내부 IP
  3. 마지막에 포트 8888을 전달하지만 적어도 것은 방화벽을 확인해야한다 전달하여 포트를 확인 웹 서버
  4. 에 해당 트래픽을 전달하는 경로가 없습니다, 그것은 허용해야 포트 8888
  5. 은 당신의 코드를 보면, 당신이 요청을 코딩 어려운 변수가 당신이 비행
7

에 그것을 변경할 수 있도록 자체 호스팅 OWIN와 C#을 사용할 때 우분투에서이 문제를 가지고 있는지 확인 보인다. 하지만 그물에 - 나는

http://*:80 

대신

http://192.168.1.1:80 
0

션 - 브래들리 @ 유사한 문제를했다 내 .EXE 안에 설정하는 기본 주소를 설정하여 고정.

WebServer ws = new WebServer(SendResponse, "http://+:80/"); 
1

WebServer ws = new WebServer(SendResponse, "http://*:80/"); 

플러스 시작 응용 프로그램 (또는 프롬프트/비주얼 스튜디오 명령) '관리자 권한으로 실행'모드와 함께 큰 일 :

이 큰 일!