4 changed files with 22 additions and 124 deletions
@ -1,120 +0,0 @@ |
|||||
using System; |
|
||||
using System.Collections.Generic; |
|
||||
using System.Linq; |
|
||||
using System.Text; |
|
||||
using System.Threading.Tasks; |
|
||||
using Newtonsoft.Json; |
|
||||
using System.IO; |
|
||||
using System.Net; |
|
||||
|
|
||||
|
|
||||
namespace formula_manage.UserClass |
|
||||
{ |
|
||||
public class HttpSERVER |
|
||||
{ |
|
||||
private static bool isExcute = true; |
|
||||
private static HttpListener listener = new HttpListener(); |
|
||||
public static void Start() |
|
||||
{ |
|
||||
System.Threading.ThreadPool.QueueUserWorkItem(w => Excute());//单独开启一个线程执行监听消息
|
|
||||
} |
|
||||
private static void Excute() |
|
||||
{ |
|
||||
if (HttpListener.IsSupported) |
|
||||
{ |
|
||||
if (!listener.IsListening) |
|
||||
{ |
|
||||
listener.Prefixes.Add("http://127.0.0.1:8888/"); //添加需要监听的url
|
|
||||
listener.Start(); //开始监听端口,接收客户端请求
|
|
||||
} |
|
||||
while (isExcute) |
|
||||
{ |
|
||||
try |
|
||||
{ |
|
||||
//阻塞主函数至接收到一个客户端请求为止 等待请求
|
|
||||
HttpListenerContext context = listener.GetContext(); |
|
||||
//解析请求
|
|
||||
HttpListenerRequest request = context.Request; |
|
||||
//构造响应
|
|
||||
HttpListenerResponse response = context.Response; |
|
||||
//http请求方式:get,post等等
|
|
||||
string httpMethod = request.HttpMethod?.ToLower(); |
|
||||
string rawUrl = request.RawUrl;//不包括IP和端口
|
|
||||
var Url = request.Url;//全路径
|
|
||||
|
|
||||
if (httpMethod == "get") |
|
||||
{ |
|
||||
//获取查询参数
|
|
||||
var queryString = request.QueryString; |
|
||||
// 请求接口 test/method?id=5
|
|
||||
//键值对方式 string val = queryString["key"];
|
|
||||
//string val = queryString["id"];val的值是5
|
|
||||
} |
|
||||
else if (httpMethod == "post") |
|
||||
{ |
|
||||
//请求接口 test/postMethod 格式是json
|
|
||||
//{
|
|
||||
// "id":5,
|
|
||||
// "name":"zs"
|
|
||||
//}
|
|
||||
//获取pots请求的请求体,json格式的字符串
|
|
||||
var reader = new StreamReader(request.InputStream); |
|
||||
var questBody = reader.ReadToEnd(); |
|
||||
if (!string.IsNullOrEmpty(rawUrl)) |
|
||||
{ |
|
||||
//这里可以直接用switch这个只是demo
|
|
||||
if (rawUrl.Equals("/server/uploadgenconnected", StringComparison.OrdinalIgnoreCase)) |
|
||||
{ |
|
||||
if (!string.IsNullOrEmpty(questBody)) |
|
||||
{ |
|
||||
UploadGenConnectedModel model = JsonConvert.DeserializeObject<UploadGenConnectedModel>(questBody); |
|
||||
if (model != null) |
|
||||
{ |
|
||||
// To Do
|
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
var responseString = string.Empty; |
|
||||
// 执行其他业务逻辑
|
|
||||
//*****************
|
|
||||
responseString = JsonConvert.SerializeObject(new { code = 1, msg = "发送成功" }); |
|
||||
byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); |
|
||||
//对客户端输出相应信息.
|
|
||||
response.ContentLength64 = buffer.Length; |
|
||||
//response.StatusCode = 200;
|
|
||||
//response.ContentType = "text/plain";
|
|
||||
//发送响应
|
|
||||
using (System.IO.Stream output = response.OutputStream) |
|
||||
{ |
|
||||
output.Write(buffer, 0, buffer.Length); |
|
||||
} |
|
||||
} |
|
||||
catch (Exception exceotion) |
|
||||
{ |
|
||||
//Logger.Error("处理消息异常", exceotion);
|
|
||||
string str = exceotion.Message; |
|
||||
} |
|
||||
} |
|
||||
} |
|
||||
else |
|
||||
{ |
|
||||
//Logger.Info("系统不支持HttpListener");
|
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public static void Stop() |
|
||||
{ |
|
||||
isExcute = false; |
|
||||
if (listener.IsListening) |
|
||||
listener.Stop(); |
|
||||
} |
|
||||
} |
|
||||
|
|
||||
public class UploadGenConnectedModel |
|
||||
{ |
|
||||
public bool GenConnected { get; set; } |
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,18 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using System.Text; |
||||
|
using System.Threading.Tasks; |
||||
|
using Newtonsoft.Json; |
||||
|
using System.IO; |
||||
|
using System.Net; |
||||
|
|
||||
|
namespace formula_manage.UserClass |
||||
|
{ |
||||
|
public class TCPUDPServer |
||||
|
{ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue