Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.01 KB | None | 0 0
  1. private static void getSslStream()
  2. {
  3. try
  4. {
  5. var clientCertificate = new X509Certificate2(ClientCertificateFile, ClientCertificatePassword);
  6. var clientCertificateCollection = new X509CertificateCollection(
  7. new X509Certificate[] {
  8. clientCertificate
  9. });
  10.  
  11.  
  12. using (var client = new TcpClient(ServerHostName, ServerPort))
  13. using (var sslStream = new SslStream(client.GetStream(), false,
  14. new RemoteCertificateValidationCallback(App_CertificateValidation)))
  15. {
  16. Console.WriteLine("Client connected.");
  17.  
  18. sslStream.AuthenticateAsClient(ServerCertificateName, clientCertificateCollection, SslProtocols.Tls12, false);
  19.  
  20. Console.WriteLine("SSL authentication completed.");
  21. Console.WriteLine("SSL using local certificate {0}.", sslStream.LocalCertificate.Subject);
  22. Console.WriteLine("SSL using remote certificate {0}.", sslStream.RemoteCertificate.Subject);
  23.  
  24.  
  25.  
  26. var request = new StringBuilder();
  27.  
  28.  
  29. request.AppendLine("GET <ipaddress with webservic method name> HTTP/1.1");
  30. request.AppendLine(string.Format("Host: {0}", hostname));
  31. request.AppendLine("Connection: close");
  32.  
  33. request.AppendLine();
  34.  
  35. var outputBuffer = Encoding.UTF8.GetBytes(request.ToString());
  36. sslStream.Write(outputBuffer);
  37. sslStream.Flush();
  38. Console.WriteLine("Sent: {0}", outputBuffer);
  39.  
  40.  
  41.  
  42. var inputBuffer = new byte[4096];
  43. var inputBytes = 0;
  44. while (inputBytes == 0)
  45. {
  46. inputBytes = sslStream.Read(inputBuffer, 0, inputBuffer.Length);
  47. }
  48. var inputMessage = Encoding.UTF8.GetString(inputBuffer, 0, inputBytes);
  49. Console.WriteLine("Received: {0}", inputMessage);
  50.  
  51. }
  52. }
  53. catch (Exception ex)
  54. {
  55. Console.WriteLine("*** {0}n*** {1}!", ex.GetType().Name, ex.Message);
  56. }
  57.  
  58. Console.WriteLine();
  59. Console.WriteLine("Press any key to continue...");
  60. Console.ReadKey();
  61. }
  62.  
  63. private static void postSslStream()
  64. {
  65. try
  66. {
  67. var clientCertificate = new X509Certificate2(ClientCertificateFile, ClientCertificatePassword);
  68. var clientCertificateCollection = new X509CertificateCollection(
  69. new X509Certificate[] {
  70. clientCertificate
  71. });
  72.  
  73.  
  74. using (var client = new TcpClient(ServerHostName, ServerPort))
  75. using (var sslStream = new SslStream(client.GetStream(), false,
  76. new RemoteCertificateValidationCallback(App_CertificateValidation)))
  77. {
  78. Console.WriteLine("Client connected.");
  79.  
  80. sslStream.AuthenticateAsClient(ServerCertificateName, clientCertificateCollection, SslProtocols.Tls12, false);
  81.  
  82. Console.WriteLine("SSL authentication completed.");
  83. Console.WriteLine("SSL using local certificate {0}.", sslStream.LocalCertificate.Subject);
  84. Console.WriteLine("SSL using remote certificate {0}.", sslStream.RemoteCertificate.Subject);
  85.  
  86. var request = new StringBuilder();
  87. var json = "{"info1": [{"info2":1,"info3":2}]}";
  88.  
  89. request.AppendLine("POST <web service method> HTTP/1.1");
  90. request.AppendLine(string.Format("Host: {0}", hostname));
  91. request.AppendLine("Content-Type: application/json");
  92. request.AppendLine("Cache-Control: no-cache");
  93. request.AppendLine("username: username");
  94. request.AppendLine("password: password");
  95. request.AppendLine(string.Format("Body: {0}", json));
  96. request.AppendLine("Connection: close");
  97. request.AppendLine();
  98.  
  99. var outputBuffer = Encoding.UTF8.GetBytes(request.ToString());
  100. sslStream.Write(outputBuffer);
  101. sslStream.Flush();
  102. Console.WriteLine("Sent: {0}", outputBuffer);
  103.  
  104.  
  105. var inputBuffer = new byte[4096];
  106. var inputBytes = 0;
  107. while (inputBytes == 0)
  108. {
  109. inputBytes = sslStream.Read(inputBuffer, 0, inputBuffer.Length);
  110. }
  111. var inputMessage = Encoding.UTF8.GetString(inputBuffer, 0, inputBytes);
  112. Console.WriteLine("Received: {0}", inputMessage);
  113.  
  114. }
  115. }
  116. catch (Exception ex)
  117. {
  118. Console.WriteLine("*** {0}n*** {1}!", ex.GetType().Name, ex.Message);
  119. }
  120.  
  121. Console.WriteLine();
  122. Console.WriteLine("Press any key to continue...");
  123. Console.ReadKey();
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement