private static string _listenerUri = "http://localhost:8881/";
private HttpListener _listener;
private ChromiumWebBrowser borwser;
public MainWindow()
{
InitializeComponent();
_listener = new HttpListener();
_listener.Prefixes.Add("http://localhost:8881/");
_listener.Start();
_listener.BeginGetContext(ListenerHandle, _listener);
WriteToStatus("启用数据监听!");
DataContext = this;
var settings = new CefSettings();
settings.BrowserSubprocessPath = System.IO.Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
Environment.Is64BitProcess ? "x64\\" : "x86\\",
"CefSharp.BrowserSubprocess.exe");
settings.MultiThreadedMessageLoop = true;
settings.CefCommandLineArgs.Add("--disable-web-security", "");
Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
borwser = new ChromiumWebBrowser(@"F:\smartInspection\SmartInspection\SmartInspection\bin\Debug\oneMachine-x64\resources\app\index.html");
this.grid.Children.Add(borwser);
}
private void ListenerHandle(IAsyncResult result)
{
try
{
string resultString = string.Empty;
if (_listener.IsListening)
{
_listener.BeginGetContext(ListenerHandle, result);
HttpListenerContext context = _listener.EndGetContext(result);
HttpListenerRequest request = context.Request;
string content = "";
switch (request.HttpMethod)
{
case "POST":
{
Stream stream = context.Request.InputStream;
StreamReader reader = new StreamReader(stream, Encoding.UTF8);
content = reader.ReadToEnd();
WriteToStatus($"收到的Post:{content}");
var uri = new Uri(request.Url.ToString());
var collection = HttpUtility.ParseQueryString(uri.Query);
resultString = PostMethodRequestContent(uri.LocalPath, content);
}
break;
case "GET":
{
var data = request.QueryString;
var uri = new Uri(request.Url.ToString());
var collection = HttpUtility.ParseQueryString(uri.Query);
WriteToStatus($"收到的GET:id={collection["id"]}data={collection["data"]}");
resultString = ReturnRequestContent(uri.LocalPath, collection);
}
break;
}
HttpListenerResponse response = context.Response;
response.StatusCode = 200;
response.ContentType = "application/json;charset=UTF-8";
response.ContentEncoding = Encoding.UTF8;
response.AppendHeader("Content-Type", "application/json;charset=UTF-8");
using (StreamWriter writer = new StreamWriter(response.OutputStream, Encoding.UTF8))
{
writer.Write(resultString);
writer.Close();
response.Close();
}
}
}
catch (Exception ex)
{
WriteToStatus(ex.Message);
}
}
public string ReturnRequestContent(string localUrl, NameValueCollection parm)
{
var result = new MessageContentModel();
switch (localUrl)
{
case "/GetStationInfoById":
{
result = GetSatationInfoById( parm);
result.UrlName = localUrl;
}
break;
default:
break;
}
return JsonConvert.SerializeObject(result);
}
private string PostMethodRequestContent(string localUrl,string parm)
{
var result = new MessageContentModel();
switch (localUrl)
{
case "/UpdataStationInfoById":
{
result = UpdateSatationInfoById(parm);
result.UrlName = localUrl;
}
break;
case "/Test2":
{
result.Content = @"C:\Users\zjy25\Documents\WeChat Files\zjy25266468\FileStorage\File\2022-03\test.html";
result.UrlName = localUrl;
}
break;
default:
break;
}
return JsonConvert.SerializeObject(result);
}
|