public async void Report() { try { JavaScriptSerializer json = new JavaScriptSerializer(); HttpClient client = new HttpClient(); HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, ReportUrl) { Content = new StringContent(json.Serialize(_gatherData()), Encoding.UTF8, "application/json") }; HttpResponseMessage res = await client.SendAsync(req); _plugin.WriteLog(ELogType.Trace, $"Analytics report: {res.StatusCode}"); req.Dispose(); res.Dispose(); client.Dispose(); } catch (Exception ex) { string errMsg = ex.Message; Exception inner = ex; while ((inner = inner.InnerException) != null) { errMsg += $" [{inner.Message}]"; } _plugin.WriteLog(ELogType.Trace, $"Analytics report: {errMsg}"); } }
public PowerwallClient(string ipAddress, ushort port, HSPI hs, string email, string password) { _ipAddress = ipAddress; _port = port; HttpClientHandler handler = new HttpClientHandler(); _httpClient = new HttpClient(handler); _jsonSerializer = new JavaScriptSerializer(); _hs = hs; _email = email; _password = password; UpstreamIpAddress = ""; // Powerwall Gateway uses a self-signed certificate, so let's accept it unconditionally handler.ServerCertificateCustomValidationCallback = (sender, certificate, chain, sslPolicyErrors) => { if (certificate.SubjectName.Name != null) { try { string commonName = certificate.SubjectName.Name .Split(',') .Select(i => i.Trim()) .First(i => i.StartsWith("CN=")) .Substring(3); if (commonName.StartsWith("TLSPROXY:")) { string upstreamIp = commonName.Substring(9); if (upstreamIp != UpstreamIpAddress) { UpstreamIpAddress = upstreamIp; _hs.WriteLog(ELogType.Info, $"Detected TLS proxy for upstream address {UpstreamIpAddress}"); } } } catch (Exception) { // No CN, ignore } } // Trust certificate always return(true); }; handler.UseCookies = true; ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12; }