Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static void getSslStream()
- {
- try
- {
- var clientCertificate = new X509Certificate2(ClientCertificateFile, ClientCertificatePassword);
- var clientCertificateCollection = new X509CertificateCollection(
- new X509Certificate[] {
- clientCertificate
- });
- using (var client = new TcpClient(ServerHostName, ServerPort))
- using (var sslStream = new SslStream(client.GetStream(), false,
- new RemoteCertificateValidationCallback(App_CertificateValidation)))
- {
- Console.WriteLine("Client connected.");
- sslStream.AuthenticateAsClient(ServerCertificateName, clientCertificateCollection, SslProtocols.Tls12, false);
- Console.WriteLine("SSL authentication completed.");
- Console.WriteLine("SSL using local certificate {0}.", sslStream.LocalCertificate.Subject);
- Console.WriteLine("SSL using remote certificate {0}.", sslStream.RemoteCertificate.Subject);
- var request = new StringBuilder();
- request.AppendLine("GET <ipaddress with webservic method name> HTTP/1.1");
- request.AppendLine(string.Format("Host: {0}", hostname));
- request.AppendLine("Connection: close");
- request.AppendLine();
- var outputBuffer = Encoding.UTF8.GetBytes(request.ToString());
- sslStream.Write(outputBuffer);
- sslStream.Flush();
- Console.WriteLine("Sent: {0}", outputBuffer);
- var inputBuffer = new byte[4096];
- var inputBytes = 0;
- while (inputBytes == 0)
- {
- inputBytes = sslStream.Read(inputBuffer, 0, inputBuffer.Length);
- }
- var inputMessage = Encoding.UTF8.GetString(inputBuffer, 0, inputBytes);
- Console.WriteLine("Received: {0}", inputMessage);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("*** {0}n*** {1}!", ex.GetType().Name, ex.Message);
- }
- Console.WriteLine();
- Console.WriteLine("Press any key to continue...");
- Console.ReadKey();
- }
- private static void postSslStream()
- {
- try
- {
- var clientCertificate = new X509Certificate2(ClientCertificateFile, ClientCertificatePassword);
- var clientCertificateCollection = new X509CertificateCollection(
- new X509Certificate[] {
- clientCertificate
- });
- using (var client = new TcpClient(ServerHostName, ServerPort))
- using (var sslStream = new SslStream(client.GetStream(), false,
- new RemoteCertificateValidationCallback(App_CertificateValidation)))
- {
- Console.WriteLine("Client connected.");
- sslStream.AuthenticateAsClient(ServerCertificateName, clientCertificateCollection, SslProtocols.Tls12, false);
- Console.WriteLine("SSL authentication completed.");
- Console.WriteLine("SSL using local certificate {0}.", sslStream.LocalCertificate.Subject);
- Console.WriteLine("SSL using remote certificate {0}.", sslStream.RemoteCertificate.Subject);
- var request = new StringBuilder();
- var json = "{"info1": [{"info2":1,"info3":2}]}";
- request.AppendLine("POST <web service method> HTTP/1.1");
- request.AppendLine(string.Format("Host: {0}", hostname));
- request.AppendLine("Content-Type: application/json");
- request.AppendLine("Cache-Control: no-cache");
- request.AppendLine("username: username");
- request.AppendLine("password: password");
- request.AppendLine(string.Format("Body: {0}", json));
- request.AppendLine("Connection: close");
- request.AppendLine();
- var outputBuffer = Encoding.UTF8.GetBytes(request.ToString());
- sslStream.Write(outputBuffer);
- sslStream.Flush();
- Console.WriteLine("Sent: {0}", outputBuffer);
- var inputBuffer = new byte[4096];
- var inputBytes = 0;
- while (inputBytes == 0)
- {
- inputBytes = sslStream.Read(inputBuffer, 0, inputBuffer.Length);
- }
- var inputMessage = Encoding.UTF8.GetString(inputBuffer, 0, inputBytes);
- Console.WriteLine("Received: {0}", inputMessage);
- }
- }
- catch (Exception ex)
- {
- Console.WriteLine("*** {0}n*** {1}!", ex.GetType().Name, ex.Message);
- }
- Console.WriteLine();
- Console.WriteLine("Press any key to continue...");
- Console.ReadKey();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement