public void Cleanup() { var URI = HTTPPath.Parse("/RNs"); String[] RoamingNetworkIds = null; using (var HTTPTask = _HTTPClient.Execute(client => client.GET(URI, requestbuilder => { requestbuilder.Host = HTTPHostname.Localhost; requestbuilder.ContentType = HTTPContentType.JSON_UTF8; requestbuilder.Accept.Add(HTTPContentType.JSON_UTF8); }), RequestTimeout: Timeout, CancellationToken: new CancellationTokenSource().Token)) { HTTPTask.Wait(Timeout); using (var HTTPResult = HTTPTask.Result) { Assert.AreEqual(HTTPStatusCode.OK, HTTPResult.HTTPStatusCode); RoamingNetworkIds = JArray.Parse(HTTPResult.HTTPBody.ToUTF8String()). AsEnumerable(). Select(v => (v as JObject)["RoamingNetworkId"].Value <String>()). ToArray(); } } foreach (var RoamingNetworkId in RoamingNetworkIds) { URI = HTTPPath.Parse("/RNs/" + RoamingNetworkId); using (var HTTPTask = _HTTPClient.Execute(client => client.DELETE(URI, requestbuilder => { requestbuilder.Host = HTTPHostname.Localhost; requestbuilder.ContentType = HTTPContentType.JSON_UTF8; requestbuilder.Accept.Add(HTTPContentType.JSON_UTF8); }), RequestTimeout: Timeout, CancellationToken: new CancellationTokenSource().Token)) { HTTPTask.Wait(Timeout); using (var HTTPResult = HTTPTask.Result) { Assert.AreEqual(HTTPStatusCode.OK, HTTPResult.HTTPStatusCode); } } } if (RemoteAddress == IPv4Address.Localhost) { HTTPAPI.Shutdown(); } }
/// <summary> /// Main. /// </summary> /// <param name="myArgs">The arguments.</param> public static void Main(String[] myArgs) { #region Start TCPServers //var _TCPServer1 = new TCPServer(new IPv4Address(new Byte[] { 192, 168, 178, 31 }), new IPPort(2001), NewConnection => // { // NewConnection.WriteToResponseStream("Hello world!" + Environment.NewLine + Environment.NewLine); // NewConnection.Close(); // }, true); //Console.WriteLine(_TCPServer1); // The first line of the repose will be served from the CustomTCPConnection handler. var _TCPServer2 = new TCPServer<CustomTCPConnection>(IPv4Address.Any, new IPPort(2002), NewConnection => { NewConnection.WriteToResponseStream("...world!" + Environment.NewLine + Environment.NewLine); NewConnection.Close(); }, true); Console.WriteLine(_TCPServer2); #endregion #region Start HTTPServers // Although the socket listens on IPv4Address.Any the service is // configured to serve clients only on http://localhost:8181 // More details within DefaultHTTPService.cs var _HTTPServer1 = new HTTPServer(IPv4Address.Any, new IPPort(8181), Autostart: true) { ServerName = "Default Hermod Demo" }; Console.WriteLine(_HTTPServer1); // This service uses a custom HTTPService defined within IRESTService.cs var _HTTPServer2 = new HTTPServer<IRESTService>(IPv4Address.Any, IPPort.HTTP, Autostart: true) { ServerName = "Customized Hermod Demo" }; Console.WriteLine(_HTTPServer2); #endregion #region UDP Servers var _UDPServer1 = new UDPServer(new IPPort(5555), NewPacket => { //NewPacket.Data = new Byte[10]; // NewPacket.Close(); }, true); #endregion var _client1 = new HTTPClient(IPv4Address.Localhost, IPPort.HTTP); var _request0 = _client1.GET("/HelloWorld"). SetHost("localhorst"). AddAccept(HTTPContentType.TEXT_UTF8, 1); var _request1 = _client1.GET("/HelloWorld"). SetHost("localhorst"). AddAccept(HTTPContentType.HTML_UTF8, 1); //WriteRequest(_request0.EntireRequestHeader); //_client1.Execute(_request0, response => WriteResponse(response.Content.ToUTF8String())). // ContinueWith(HTTPClient => { WriteRequest(_request1.EntireRequestHeader); return HTTPClient.Result; }). // ContinueWith(HTTPClient => HTTPClient.Result.Execute(_request1, response => WriteResponse(response.Content.ToUTF8String()))). // Wait(); var _client2 = new HTTPClient(IPv4Address.Parse("188.40.47.229"), IPPort.HTTP); var _requestA = _client2.GET("/"). SetProtocolVersion(HTTPVersion.HTTP_1_1). SetHost("www.ahzf.de"). SetUserAgent("Hermod HTTP Client v0.1"). SetConnection("keep-alive"). AddAccept(HTTPContentType.HTML_UTF8, 1); var _requestB = _client2.GET("/nfgj"). SetProtocolVersion(HTTPVersion.HTTP_1_1). SetHost("www.ahzf.de"). SetUserAgent("Hermod HTTP Client v0.1"). SetConnection("keep-alive"). AddAccept(HTTPContentType.HTML_UTF8, 1); WriteRequest(_requestA); _client2.Execute(_requestA, response => WriteResponse(response)). ContinueWith(Client => Client.Result.Execute(_requestB, response => WriteResponse(response))); var _req23a = new HTTPRequestBuilder(). SetHTTPMethod (HTTPMethod.GET). SetProtocolName ("µHTTP"). SetProtocolVersion (new HTTPVersion(2, 0)). SetHost ("localhorst"). SetUserAgent ("Hermod µHTTP Client"). SetContent ("This the HTTP content..."); _req23a.QueryString.Add("name", "alice"). Add("friend", "bob"). Add("friend", "carol"); var _req23b = new HTTPRequestBuilder() { HTTPMethod = HTTPMethod.GET, ProtocolName = "µHTTP", ProtocolVersion = new HTTPVersion(2, 0), Host = "localhorst", UserAgent = "Hermod µHTTP Client", Content = "This the HTTP content...".ToUTF8Bytes() }; // var Response = new TCPClientRequest("localhost", 80).Send("GETTT / HTTP/1.1").FinishCurrentRequest().Response; Console.ReadLine(); Console.WriteLine("done!"); }
/// <summary> /// Main. /// </summary> /// <param name="myArgs">The arguments.</param> public static void Main(String[] myArgs) { #region Start TCPServers //var _TCPServer1 = new TCPServer(new IPv4Address(new Byte[] { 192, 168, 178, 31 }), new IPPort(2001), NewConnection => // { // NewConnection.WriteToResponseStream("Hello world!" + Environment.NewLine + Environment.NewLine); // NewConnection.Close(); // }, true); //Console.WriteLine(_TCPServer1); // The first line of the repose will be served from the CustomTCPConnection handler. var _TCPServer2 = new TCPServer <CustomTCPConnection>(IPv4Address.Any, new IPPort(2002), NewConnection => { NewConnection.WriteToResponseStream("...world!" + Environment.NewLine + Environment.NewLine); NewConnection.Close(); }, true); Console.WriteLine(_TCPServer2); #endregion #region Start HTTPServers // Although the socket listens on IPv4Address.Any the service is // configured to serve clients only on http://localhost:8181 // More details within DefaultHTTPService.cs var _HTTPServer1 = new HTTPServer(IPv4Address.Any, new IPPort(8181), Autostart: true) { ServerName = "Default Hermod Demo" }; Console.WriteLine(_HTTPServer1); // This service uses a custom HTTPService defined within IRESTService.cs var _HTTPServer2 = new HTTPServer <IRESTService>(IPv4Address.Any, IPPort.HTTP, Autostart: true) { ServerName = "Customized Hermod Demo" }; Console.WriteLine(_HTTPServer2); #endregion #region UDP Servers var _UDPServer1 = new UDPServer(new IPPort(5555), NewPacket => { //NewPacket.Data = new Byte[10]; // NewPacket.Close(); }, true); #endregion var _client1 = new HTTPClient(IPv4Address.Localhost, IPPort.HTTP); var _request0 = _client1.GET("/HelloWorld"). SetHost("localhorst"). AddAccept(HTTPContentType.TEXT_UTF8, 1); var _request1 = _client1.GET("/HelloWorld"). SetHost("localhorst"). AddAccept(HTTPContentType.HTML_UTF8, 1); //WriteRequest(_request0.EntireRequestHeader); //_client1.Execute(_request0, response => WriteResponse(response.Content.ToUTF8String())). // ContinueWith(HTTPClient => { WriteRequest(_request1.EntireRequestHeader); return HTTPClient.Result; }). // ContinueWith(HTTPClient => HTTPClient.Result.Execute(_request1, response => WriteResponse(response.Content.ToUTF8String()))). // Wait(); var _client2 = new HTTPClient(IPv4Address.Parse("188.40.47.229"), IPPort.HTTP); var _requestA = _client2.GET("/"). SetProtocolVersion(HTTPVersion.HTTP_1_1). SetHost("www.ahzf.de"). SetUserAgent("Hermod HTTP Client v0.1"). SetConnection("keep-alive"). AddAccept(HTTPContentType.HTML_UTF8, 1); var _requestB = _client2.GET("/nfgj"). SetProtocolVersion(HTTPVersion.HTTP_1_1). SetHost("www.ahzf.de"). SetUserAgent("Hermod HTTP Client v0.1"). SetConnection("keep-alive"). AddAccept(HTTPContentType.HTML_UTF8, 1); WriteRequest(_requestA); _client2.Execute(_requestA, response => WriteResponse(response)). ContinueWith(Client => Client.Result.Execute(_requestB, response => WriteResponse(response))); var _req23a = new HTTPRequestBuilder(). SetHTTPMethod(HTTPMethod.GET). SetProtocolName("µHTTP"). SetProtocolVersion(new HTTPVersion(2, 0)). SetHost("localhorst"). SetUserAgent("Hermod µHTTP Client"). SetContent("This the HTTP content..."); _req23a.QueryString.Add("name", "alice"). Add("friend", "bob"). Add("friend", "carol"); var _req23b = new HTTPRequestBuilder() { HTTPMethod = HTTPMethod.GET, ProtocolName = "µHTTP", ProtocolVersion = new HTTPVersion(2, 0), Host = "localhorst", UserAgent = "Hermod µHTTP Client", Content = "This the HTTP content...".ToUTF8Bytes() }; // var Response = new TCPClientRequest("localhost", 80).Send("GETTT / HTTP/1.1").FinishCurrentRequest().Response; Console.ReadLine(); Console.WriteLine("done!"); }
public void Test_ChargingPools() { var HTTPClient = new HTTPClient(IPv4Address.Localhost, RemotePort: IPPort.Parse(8000), DNSClient: HTTPAPI.DNSClient); var RN = OpenChargingCloudAPI.CreateNewRoamingNetwork(Id: RoamingNetwork_Id.Parse("TEST_RN1"), Name: I18NString.Create(Languages.deu, "Test Roaming Netz 1"). Add(Languages.eng, "Test roaming network 1")); var CPO = RN.CreateChargingStationOperator(ChargingStationOperatorId: ChargingStationOperator_Id.Parse("DE*GEF"), Name: I18NString.Create(Languages.deu, "GraphDefined"), Description: I18NString.Create(Languages.deu, "GraphDefined EVSE Operator"), Configurator: evseoperator => { evseoperator.AddDataLicense(DataLicense.OpenDatabaseLicense); }); #region Verify GET /RNs/TEST_RN1/ChargingPools var URI = HTTPPath.Parse("/RNs/TEST_RN1/ChargingPools"); using (var HTTPTask = HTTPClient.Execute(client => client.GET(URI, requestbuilder => { requestbuilder.Host = HTTPHostname.Localhost; requestbuilder.ContentType = HTTPContentType.JSON_UTF8; requestbuilder.Accept.Add(HTTPContentType.JSON_UTF8); }), RequestTimeout: Timeout, CancellationToken: new CancellationTokenSource().Token)) { HTTPTask.Wait(Timeout); using (var HTTPResult = HTTPTask.Result) { Assert.AreEqual(HTTPStatusCode.OK, HTTPResult.HTTPStatusCode, "'GET " + URI + "' failed!"); Assert.AreEqual(new JArray().ToString(), JArray.Parse(HTTPResult.HTTPBody.ToUTF8String()).ToString(), "Invalid response for 'GET " + URI + "'!"); } } #endregion #region Verify COUNT /RNs/TEST_RN1/ChargingPools using (var HTTPTask = HTTPClient.Execute(client => client.COUNT(URI, requestbuilder => { requestbuilder.Host = HTTPHostname.Localhost; requestbuilder.ContentType = HTTPContentType.JSON_UTF8; requestbuilder.Accept.Add(HTTPContentType.JSON_UTF8); }), RequestTimeout: Timeout, CancellationToken: new CancellationTokenSource().Token)) { HTTPTask.Wait(Timeout); using (var HTTPResult = HTTPTask.Result) { Assert.AreEqual(HTTPStatusCode.OK, HTTPResult.HTTPStatusCode, "'GET " + URI + "' failed!"); Assert.AreEqual(new JObject(new JProperty("count", 0)).ToString(), JObject.Parse(HTTPResult.HTTPBody.ToUTF8String()).ToString(), "Invalid response for 'COUNT " + URI + "'!"); } } #endregion var Pool_1 = CPO.CreateChargingPool(ChargingPool_Id.Parse(CPO.Id, "1111"), pool => { pool.Address = Address.Create(Country.Austria, "07741", I18NString.Create(Languages.deu, "Wien"), "Hofplatz", "17"); }); var Pool_2 = CPO.CreateChargingPool(ChargingPool_Id.Parse(CPO.Id, "2222"), pool => { pool.Address = Address.Create(Country.Germany, "07749", I18NString.Create(Languages.deu, "Jena"), "Biberweg", "18"); }); var Pool_3 = CPO.CreateChargingPool(ChargingPool_Id.Parse(CPO.Id, "3333"), pool => { pool.Address = Address.Create(Country.Belgium, "07758", I18NString.Create(Languages.bgn, "Brussels"), "Avenue", "19"); }); #region Verify GET /RNs/TEST_RN1/ChargingPools URI = HTTPPath.Parse("/RNs/TEST_RN1/ChargingPools"); using (var HTTPTask = HTTPClient.Execute(client => client.GET(URI, requestbuilder => { requestbuilder.Host = HTTPHostname.Localhost; requestbuilder.ContentType = HTTPContentType.JSON_UTF8; requestbuilder.Accept.Add(HTTPContentType.JSON_UTF8); }), RequestTimeout: Timeout, CancellationToken: new CancellationTokenSource().Token)) { HTTPTask.Wait(Timeout); using (var HTTPResult = HTTPTask.Result) { Assert.AreEqual(HTTPStatusCode.OK, HTTPResult.HTTPStatusCode, "'GET " + URI + "' failed!"); Assert.AreEqual(new JArray( new JObject(new JProperty("ChargingPoolId", "DE*GEF*P1111"), new JProperty("OperatorId", "DE*GEF"), new JProperty("Address", new JObject(new JProperty("houseNumber", "17"), new JProperty("street", "Hofplatz"), new JProperty("postalCode", "07741"), new JProperty("city", new JObject(new JProperty("de", "Wien")) ), new JProperty("country", new JObject(new JProperty("en", "Austria")) ) )), new JProperty("ChargingStations", new JArray()) ), new JObject(new JProperty("ChargingPoolId", "DE*GEF*P2222"), new JProperty("OperatorId", "DE*GEF"), new JProperty("Address", new JObject(new JProperty("houseNumber", "18"), new JProperty("street", "Biberweg"), new JProperty("postalCode", "07749"), new JProperty("city", new JObject(new JProperty("de", "Jena") )), new JProperty("country", new JObject( new JProperty("en", "Germany"), new JProperty("de", "Deutschland") ) ) )), new JProperty("ChargingStations", new JArray()) ), new JObject(new JProperty("ChargingPoolId", "DE*GEF*P3333"), new JProperty("OperatorId", "DE*GEF"), new JProperty("Address", new JObject(new JProperty("houseNumber", "19"), new JProperty("street", "Avenue"), new JProperty("postalCode", "07758"), new JProperty("city", new JObject(new JProperty("be", "Brussels")) ), new JProperty("country", new JObject(new JProperty("en", "Belgium")) ) )), new JProperty("ChargingStations", new JArray()) ) ).ToString(), JArray.Parse(HTTPResult.HTTPBody.ToUTF8String()).ToString(), "Invalid response for 'GET " + URI + "'!"); } } #endregion #region Verify COUNT /RNs/TEST_RN1/ChargingPools using (var HTTPTask = HTTPClient.Execute(client => client.COUNT(URI, requestbuilder => { requestbuilder.Host = HTTPHostname.Localhost; requestbuilder.ContentType = HTTPContentType.JSON_UTF8; requestbuilder.Accept.Add(HTTPContentType.JSON_UTF8); }), RequestTimeout: Timeout, CancellationToken: new CancellationTokenSource().Token)) { HTTPTask.Wait(Timeout); using (var HTTPResult = HTTPTask.Result) { Assert.AreEqual(HTTPStatusCode.OK, HTTPResult.HTTPStatusCode, "'GET " + URI + "' failed!"); Assert.AreEqual(new JObject(new JProperty("count", 3)).ToString(), JObject.Parse(HTTPResult.HTTPBody.ToUTF8String()).ToString(), "Invalid response for 'COUNT " + URI + "'!"); } } #endregion }