WebSocketClientTest.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Security;
  5. using System.Net.WebSockets;
  6. using System.Security.Cryptography;
  7. using System.Security.Cryptography.X509Certificates;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace ConsoleApp1.WebSocketClient
  11. {
  12. internal static class WebSocketClientTest
  13. {
  14. public static async Task Test()
  15. {
  16. var url = new Uri("wss://localhost/PHSimulator006501");
  17. var clientCertificate = X509Certificate2.CreateFromPemFile("crt.cert.pem", "key.key.pem");
  18. var tmpCert = new X509Certificate2(clientCertificate.Export(X509ContentType.Pfx));
  19. //var clientCertificate = new X509Certificate2("PHSimulator006501.pfx", "test");
  20. //var clienCertificateRoot = X509Certificate2.CreateFromPemFile("rca.crt");
  21. var client = new ClientWebSocket();
  22. client.Options.AddSubProtocol("ocpp1.6");
  23. client.Options.ClientCertificates.Add(tmpCert);
  24. client.Options.RemoteCertificateValidationCallback = new RemoteCertificateValidationCallback(ValidateRemoteCertificate);
  25. //client.Options.ClientCertificates.Add(clienCertificateRoot);
  26. await client.ConnectAsync(url, default);
  27. }
  28. private static bool ValidateRemoteCertificate(object sender, X509Certificate? certificate, X509Chain? chain, SslPolicyErrors sslPolicyErrors)
  29. {
  30. return true;
  31. }
  32. }
  33. }