Files
bodyshop-uploader/BodyshopUploader/Utils/HTTPServer.cs
2020-05-28 09:08:34 -07:00

66 lines
2.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SimpleHttp;
namespace BodyshopPartner.Utils
{
public static class HTTPServer
{
private static NLog.Logger logger = NLog.LogManager.GetCurrentClassLogger();
public static void InitHttpServer()
{
Route.Add("/qb/receivables", (req, res, props) =>
{
res.WithCORS();
//res.AddHeader("Access-Control-Allow-Origin", "http://localhost:3000 https://localhost:3000 http://localhost:5000 https://localhost:5000 https://*.imex.online,imex.online");
res.Close();
}, "OPTIONS");
Route.Add("/qb/receivables", (req, res, props) =>
{
logger.Debug("/qb/receivables - POST");
string AuthToken = req.Headers.Get("Authorization");
string requestXML = ParseBody(req);
logger.Trace("Incoming Request XML", requestXML);
//res.WithCORS();
Utils.QuickBooksInterop.ConnectToQuickBooks();
string result = Utils.QuickBooksInterop.processRequestFromQB(requestXML);
Utils.QuickBooksInterop.DisconnectFromQuickBooks();
res.WithCORS().AsText("Hello.");
}, "POST");
logger.Trace("Starting HTTP server...");
//TODO As a part of the installer, add netsh http add urlacl url=http://+:1337/ user="Everyone
HttpServer.ListenAsync(1337, System.Threading.CancellationToken.None, Route.OnHttpRequestAsync).Wait();
}
public static string ParseBody(System.Net.HttpListenerRequest req)
{
System.IO.Stream body = req.InputStream;
System.Text.Encoding encoding = req.ContentEncoding;
System.IO.StreamReader reader = new System.IO.StreamReader(body, encoding);
if (req.ContentType != null)
{
Console.WriteLine("Client data content type {0}", req.ContentType);
}
Console.WriteLine("Client data content length {0}", req.ContentLength64);
Console.WriteLine("Start of client data:");
// Convert the data to a string and display it on the console.
string s = reader.ReadToEnd();
Console.WriteLine(s);
Console.WriteLine("End of client data:");
body.Close();
return s;
}
}
}