Из андроида пошлите запрос
POST https://you_server_api/api/power
нагрузка запроса
{
"mode": "on"
}
В приложении создайте веб сервер
var baseAdress = new Uri(Properties.Settings.Default.BaseUri);
var server = new WebApiServer(baseAdress);
server.Start();
var message = $"Web API Self hosted on {baseAdress} Hit ENTER to exit...";
_logger.Info(message);
System.Console.ReadLine();
server.Stop();
return (int)ScanerStatus.Ok;
using NLog;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.SelfHost;
namespace RO.WebScaner.Console
{
/// <summary>
/// Web api server implementation
/// </summary>
public class WebApiServer
{
Logger _logger = LogManager.GetCurrentClassLogger();
Uri _uri;
HttpSelfHostServer _server;
/// <summary>
/// Create instance of Webapi embedd server
/// </summary>
/// <param name="uri">Endpoint address</param>
public WebApiServer(Uri uri)
{
_uri = uri;
}
/// <summary>
/// Start web api server.
/// </summary>
/// Команда для запуска исключения файрвола
/// netsh http add urlacl url=http://+:8000/ user=ZOOPLAZA-PC\developer
/// <remarks>
/// </remarks>
public void Start()
{
_logger.Info("Start WebApi server");
HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(_uri);
config.EnableCors();
ConfigRoutes(config);
ConfigFormatter(config);
_server = new HttpSelfHostServer(config);
_server.OpenAsync().Wait();
}
/// <summary>
/// Remove xml formatter, maybe in future add binary formatter
/// </summary>
/// <param name="config"></param>
private static void ConfigFormatter(HttpSelfHostConfiguration config)
{
// remove xml
var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
}
/// <summary>
/// Config web api routes
/// </summary>
/// <param name="config"></param>
private static void ConfigRoutes(HttpSelfHostConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
public void Stop() {
_server.CloseAsync().Wait();
_logger.Info("Close WebApi server");
}
}
}
using NLog;
using RO.WebScaner.Console.Models;
using RO.WebScaner.Core;
using RO.WebScaner.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
namespace RO.WebScaner.Console.Controllers
{
public class ListController : ApiController
{
Logger _logger;
/// <summary>
/// Logger with delay load
/// </summary>
public Logger Logger
{
get
{
if (_logger == null)
{
_logger = LogManager.GetCurrentClassLogger();
}
return _logger;
}
set { _logger = value; }
}
[EnableCors(origins: "*",headers: "*", methods:"*")]
public IHttpActionResult Get()
{
try
{
var model = Worker.List();
return Ok(model);
}
catch(Exception e)
{
Logger.Error(e);
return InternalServerError(e);
}
}
}
}