using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace MySpace
{
class Program
{staticvoidMain(string[] args){
HttpServer httpServer = new HttpServer(16130);
httpServer.Start();// 隐藏窗口
Console.Title ="DataProcessService";HideWindow();
Thread.Sleep(System.Threading.Timeout.Infinite);}// 隐藏控制台
private staticvoidHideWindow(){ShowWindow(FindWindow(null,"DataProcessService"),0);}[DllImport("User32.dll", EntryPoint ="FindWindow")]
private staticextern IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("User32.dll", EntryPoint ="ShowWindow")]//
private staticextern bool ShowWindow(IntPtr hWnd,int type);}}
HttpServer.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
namespace MySpace
{
public class HttpServer
{
private HttpListener _listener;
public staticint _port;
public HttpServer(int port){
_port = port;}
public voidStart(){
_listener = new HttpListener();
_listener.Prefixes.Add(String.Format("http://*:{0}/", _port.ToString()));
_listener.Start();
_listener.BeginGetContext(ProcessRequest, null);}
public voidStop(){
_listener.Stop();}
private voidProcessRequest(IAsyncResult result){
HttpListenerContext context = null;
try
{
_listener.BeginGetContext(ProcessRequest, null);
context = _listener.EndGetContext(result);// 发送短信内容
var reader = new System.IO.StreamReader(context.Request.InputStream);
String data = reader.ReadToEnd();// Http回复
using (StreamWriter writer = new StreamWriter(context.Response.OutputStream)){
context.Response.StatusCode =200;
writer.Write(data);
writer.Close();}
context.Response.StatusCode =200;
context.Response.Close();}
catch (Exception e){
using (StreamWriter writer = new StreamWriter(context.Response.OutputStream)){
context.Response.StatusCode =400;
writer.Write(e.Message);
writer.Close();}}}}}