using HttpListenerPost;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace PaddleOcrService.request.myhttp
public class MyHttpServer
public delegate void respNoticeDelegate(Dictionary<string, string> data, HttpListenerResponse resp,string route,string request_type = "get");
public event respNoticeDelegate respNotice;
private HttpListener listener = new HttpListener();
private Dictionary<string, string> actionDict = new Dictionary<string, string>();
private ReturnDataBase respObj;
public string curr_path = "";
public Dictionary<string, string> data_rec = new Dictionary<string, string>();
public MyHttpServer(Dictionary<string,string> routes = null)
if(routes != null)
foreach (KeyValuePair<string, string> kvp in routes)
AddPrefixes(kvp.Key, kvp.Value);
public void AddPrefixes(string url, string action)
actionDict.Add(url, action);
public void close()
listener.Stop();
public void Start(int port)
if (!HttpListener.IsSupported)
Console.WriteLine("无法在当前系统上运行服务(Windows XP SP2 or Server 2003)。" + DateTime.Now.ToString());
return;
if (actionDict.Count <= 0)
System.Console.WriteLine("没有监听端口");
foreach (var item in actionDict)
var url = string.Format("http://127.0.0.1:{0}{1}", port, item.Key + "/");
System.Console.WriteLine(url);
listener.Prefixes.Add(url);
listener.Start();
listener.BeginGetContext(Result, null);
respObj = new ReturnDataBase();
System.Console.WriteLine("开始监听");
System.Console.Read();
private void Result(IAsyncResult asy)
if (!listener.IsListening) return;
listener.BeginGetContext(Result, null);
var context = listener.EndGetContext(asy);
var req = context.Request;
var rsp = context.Response;
string route=HandlerReq(req.RawUrl);
Dictionary<string, string> data = new Dictionary<string, string>();
data= HandleHttpMethod(context,rsp,route);
dataNoticeEvent(data,rsp, route, context.Request.HttpMethod);
public string responData(string content,HttpListenerResponse rsp)
using (var stream = rsp.OutputStream)
rsp.StatusCode = 200;
rsp.ContentType = "text/html;charset=UTF-8";
rsp.AddHeader("Content-type", "application/json");
rsp.ContentEncoding = Encoding.UTF8;
rsp.AppendHeader("Access-Control-Allow-Origin", "*");
rsp.AppendHeader("Access-Control-Allow-Credentials", "true");
rsp.AppendHeader("Access-Control-Allow-Headers", "Authorization,Content-Type,Accept,Origin,User-Agent,DNT,Cache-Control,X-Mx-ReqToken,X-Requested-With");
rsp.AppendHeader("Access-Control-Max-Age", "86400");
byte[] dataByte = Encoding.UTF8.GetBytes(content);
stream.Write(dataByte, 0, dataByte.Length);
stream.Close();
catch (Exception e)
rsp.Close();
return e.Message;
rsp.Close();
return "";
private string HandlerReq(string url)
System.Console.WriteLine("url : " + url);
string[] arr_str= url.Split('?');
if(arr_str.Length > 0)
return curr_path = arr_str[0];
return "";
catch (Exception e)
return "";
private Dictionary<string,string> HandleHttpMethod(HttpListenerContext context, HttpListenerResponse resp,string route)
Dictionary<string, string> return_data = new Dictionary<string, string>();
data_rec.Clear();
string contentType = context.Request.ContentType == null ? "" : context.Request.ContentType;
if (contentType.Contains("multipart/form-data") )
HttpListenerPostParaHelper parse = new HttpListenerPostParaHelper(context);
List<HttpListenerPostValue> list = parse.GetHttpListenerPostValue();
foreach (HttpListenerPostValue item in list)
string k = item.name;
string value = "";
if (item.type == 0)
value = Encoding.UTF8.GetString(item.datas).Replace("\r\n", "");
File.WriteAllBytes(@"D:\test.png", item.datas);
value = @"D:\test.png";
dataRecAdd(k,value);
return_data = data_rec;
return return_data;
if (contentType.Contains("application/json"))
Stream stream = context.Request.InputStream;
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
string json = reader.ReadToEnd();
Dictionary<string, string> DicContent = new Dictionary<string, string>();
if (string.IsNullOrEmpty(json)) return return_data ;
if (json == "[]" || json == "") return return_data;
data_rec = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
return_data = data_rec;
catch (Exception ex)
return return_data;
return return_data;
var data = context.Request.QueryString;
string url = context.Request.Url.ToString();
string[] pars = url.Split('?');
string content = "";
if (pars.Length == 0)
return return_data;
if (pars.Length <= 1) return return_data;
string canshus = pars[1];
if (canshus.Length > 0)
string[] canshu = canshus.Split('&');
foreach (string i in canshu)
string[] messages = i.Split('=');
dataRecAdd(messages[0], messages[1]);
return_data = data_rec;
return return_data;
break;
return return_data;
public void dataNoticeEvent(Dictionary<string, string> data,HttpListenerResponse rsp,string route, string method = "unkonwn")
respNotice?.Invoke(data, rsp, route, method);
public void dataRecAdd(string k ,string v)
if (data_rec.ContainsKey(k))
data_rec[k] = v;
data_rec.Add(k, v);
class ReturnDataBase
public string GetDataMain(string class_method, Dictionary<string, string> rec_data)
string[] class_arr = class_method.Split('.');
string class_name,method;
if (class_arr.Length == 1) class_name = class_arr[0];
if (class_arr.Length == 2) method = class_arr[1];
if (class_arr.Length == 0) return "";
return "cesh";
这个类主要用于解析content-type为multipart/form-data时解析数据
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
namespace HttpListenerPost
public class HttpListenerPostValue
public int type = 0;
public string name;
public byte[] datas;
public class HttpListenerPostParaHelper
private HttpListenerContext request;
public HttpListenerPostParaHelper(HttpListenerContext request)
this.request = request;
private bool CompareBytes(byte[] source, byte[] comparison)
int count = source.Length;
if (source.Length != comparison.Length)
return false;
for (int i = 0; i < count; i++)
if (source[i] != comparison[i])
return false;
return true;
catch
return false;
private byte[] ReadLineAsBytes(Stream SourceStream)
var resultStream = new MemoryStream();
while (true)
int data = SourceStream.ReadByte();
resultStream.WriteByte((byte)data);
if (data == 10)
break;
resultStream.Position = 0;
byte[] dataBytes = new byte[resultStream.Length];
resultStream.Read(dataBytes, 0, dataBytes.Length);
return dataBytes;
public List<HttpListenerPostValue> GetHttpListenerPostValue()
List<HttpListenerPostValue> HttpListenerPostValueList = new List<HttpListenerPostValue>();
if (request.Request.ContentType.Length > 20 && string.Compare(request.Request.ContentType.Substring(0, 20), "multipart/form-data;", true) == 0)
string[] HttpListenerPostValue = request.Request.ContentType.Split(';').Skip(1).ToArray();
string boundary = string.Join(";", HttpListenerPostValue).Replace("boundary=", "").Trim();
byte[] ChunkBoundary = Encoding.UTF8.GetBytes("--" + boundary + "\r\n");
byte[] EndBoundary = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");
Stream SourceStream = request.Request.InputStream;
var resultStream = new MemoryStream();
bool CanMoveNext = true;
HttpListenerPostValue data = null;
while (CanMoveNext)
byte[] currentChunk = ReadLineAsBytes(SourceStream);
if (!Encoding.UTF8.GetString(currentChunk).Equals("\r\n"))
resultStream.Write(currentChunk, 0, currentChunk.Length);
if (CompareBytes(ChunkBoundary, currentChunk))
byte[] result = new byte[resultStream.Length - ChunkBoundary.Length];
resultStream.Position = 0;
resultStream.Read(result, 0, result.Length);
CanMoveNext = true;
if (result.Length > 0)
data.datas = result;
data = new HttpListenerPostValue();
HttpListenerPostValueList.Add(data);
resultStream.Dispose();
resultStream = new MemoryStream();
else if (Encoding.UTF8.GetString(currentChunk).Contains("Content-Disposition"))
byte[] result = new byte[resultStream.Length - 2];
resultStream.Position = 0;
resultStream.Read(result, 0, result.Length);
CanMoveNext = true;
data.name = Encoding.UTF8.GetString(result).Replace("Content-Disposition: form-data; name=\"", "").Replace("\"", "").Split(';')[0];
resultStream.Dispose();
resultStream = new MemoryStream();
else if (Encoding.UTF8.GetString(currentChunk).Contains("Content-Type"))
CanMoveNext = true;
data.type = 1;
resultStream.Dispose();
resultStream = new MemoryStream();
else if (CompareBytes(EndBoundary, currentChunk))
byte[] result = new byte[resultStream.Length - EndBoundary.Length - 2];
resultStream.Position = 0;
resultStream.Read(result, 0, result.Length);
data.datas = result;
resultStream.Dispose();
CanMoveNext = false;
return HttpListenerPostValueList;
catch (Exception ex)
return null;
2. 在项目中添加一个新的类文件(.cs),该文件将包含Web API的控制器代码。
3. 在控制器类中,使用System.Web.Http命名空间并继承ApiController类。
4. 在控制器类中,创建要公开的API方法。你可以使用各种HTTP动词(如GET、POST、PUT、DELETE等)来处理不同的API请求。
5. 在控制器类中,使用[Route]属性来定义API方法的路由。这将决定API方法可以通过哪个URL进行访问。
6. 在控制器类中,使用[HttpGet]、[HttpPost]等属性来定义API方法的HTTP动词。
7. 在控制器类中,实现API方法的具体逻辑。这可能包括从数据库中检索数据、处理请求参数等。
8. 在WinForms应用程序的入口点(例如Main函数)中,使用System.Web.Http.SelfHost命名空间来启动Web API服务器。
9. 在Web API服务器的启动代码中,使用HttpSelfHostConfiguration类来配置服务器设置,例如指定要监听的端口号、启用跨域访问等。
10. 在Web API服务器的启动代码中,使用HttpSelfHostServer类来创建并启动服务器。
11. 运行你的C# WinForms应用程序,并确保Web API服务器已经成功启动。
现在,你的C# WinForms应用程序中就有一个Web API服务器了,可以通过API方法来处理HTTP请求。你可以使用工具(例如Postman)来测试和调试API方法。请记住,这只是一个基本的示例,你可以根据自己的需求进行更多的定制化和功能扩展。
print("\tpython")
print("languages:\npython\nC\njacascript")
print("languages:\n\tpython\n\tC\n\tjacascript")
[/code]