당신은
HttpListener
을 만들어 보길 원하는 것
, 당신 청취자에게 접두사를 추가 할 수 있습니다. 예를 들어, Listener.Prefixes.Add("http://+:3070/")
과 같이 원하는 포트에 바인드 할 수 있습니다.
간단한 콘솔 응용 프로그램 : 요청이
using System;
using System.Net;
using System.Text;
namespace TestServer
{
class ServerMain
{
// To enable this so that it can be run in a non-administrator account:
// Open an Administrator command prompt.
// netsh http add urlacl http://+:8008/ user=Everyone listen=true
const string Prefix = "http://+:3070/";
static HttpListener Listener = null;
static int RequestNumber = 0;
static readonly DateTime StartupDate = DateTime.UtcNow;
static void Main(string[] args)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine("HttpListener is not supported on this platform.");
return;
}
using (Listener = new HttpListener())
{
Listener.Prefixes.Add(Prefix);
Listener.Start();
// Begin waiting for requests.
Listener.BeginGetContext(GetContextCallback, null);
Console.WriteLine("Listening. Press Enter to stop.");
Console.ReadLine();
Listener.Stop();
}
}
static void GetContextCallback(IAsyncResult ar)
{
int req = ++RequestNumber;
// Get the context
var context = Listener.EndGetContext(ar);
// listen for the next request
Listener.BeginGetContext(GetContextCallback, null);
// get the request
var NowTime = DateTime.UtcNow;
Console.WriteLine("{0}: {1}", NowTime.ToString("R"), context.Request.RawUrl);
var responseString = string.Format("<html><body>Your request, \"{0}\", was received at {1}.<br/>It is request #{2:N0} since {3}.",
context.Request.RawUrl, NowTime.ToString("R"), req, StartupDate.ToString("R"));
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
// and send it
var response = context.Response;
response.ContentType = "text/html";
response.ContentLength64 = buffer.Length;
response.StatusCode = 200;
response.OutputStream.Write(buffer, 0, buffer.Length);
response.OutputStream.Close();
}
}
}
했다 세고 추가 신용, 컴퓨터의 서비스에 추가하십시오!
'HttpListener' : http://msdn.microsoft.com/en-us/library/system.net.httplistener(v=vs.110).aspx –
http://www.asp.net/web- 양식 – user2930100
asp.net 또는 Windows Forms에 대한 질문이 있습니까? 그들은 완전히 다릅니다 (Windows Forms는 제목에 있습니다 .. 따라서'HttpListener'에 링크 된 이유입니다). –