Advertisement
Guest User

Untitled

a guest
Jun 26th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. string host = "twitter.com";
  2.  
  3. // Connect socket
  4. TcpClient client = new TcpClient(host, 443);
  5. NetworkStream stream = client.GetStream();
  6.  
  7. // Wrap in SSL stream
  8. SslStream sslStream = new SslStream(stream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate));
  9. sslStream.AuthenticateAsClient(host);
  10.  
  11. //Build request
  12. var sb = new StringBuilder();
  13.  
  14. sb.AppendLine("GET / HTTP/1.1");
  15. sb.AppendLine(string.Format("Host: {0}", host));
  16. sb.AppendLine("Connection: keep-alive");
  17. sb.AppendLine("User-Agent: CSharp");
  18. sb.AppendLine("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
  19. sb.AppendLine("Accept-Encoding: gzip,deflate,sdch");
  20. sb.AppendLine("Accept-Language: en-US,en;q=0.8");
  21. sb.AppendLine("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3");
  22. sb.AppendLine();
  23.  
  24. //Go go go!
  25. byte[] headerBytes = Encoding.UTF8.GetBytes(sb.ToString());
  26. sslStream.Write(headerBytes, 0, headerBytes.Length);
  27. sslStream.Flush();
  28.  
  29.  
  30. //Get a place to put it
  31. byte[] buffer = new byte[2048];
  32. int bytes;
  33.  
  34. //Answer
  35. do
  36. {
  37. bytes = sslStream.Read(buffer, 0, buffer.Length);
  38. Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
  39. } while (bytes != 0);
  40.  
  41. //And done
  42. client.Close();
  43. Console.ReadKey();
  44.  
  45.  
  46. }
  47.  
  48. public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
  49. {
  50. return true;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement