title: Making HTTPS call in C# with the BouncyCastle library
body: <p>Using C# 4.0, I need to make HTTPS call with the BouncyCastle library (Short story : Windows XP + TLS 1.2).</p>
  
  <p>When using the following code, I get a <strong>"HTTP Error 400. The request verb is invalid."</strong></p>
  
  <p>Here is my code : </p>
  
  <pre><code>using (var client = new TcpClient("serverName", 443))
  {
      var sr = new SecureRandom();
      var cl = new MyTlsClient();
      var protocol = new TlsClientProtocol(client.GetStream(), sr);
      protocol.Connect(new MyTlsClient());
  
      using (var stream = protocol.Stream)
      {
           var hdr = new StringBuilder();
           hdr.AppendLine("GET /Url/WebService.asmx?wsdl HTTP/1.1");
           hdr.AppendLine("Host: serverName");
           hdr.AppendLine("Content-Type: text/xml; charset=utf-8");
           hdr.AppendLine("Connection: close");
           hdr.AppendLine();
  
           var dataToSend = Encoding.ASCII.GetBytes(hdr.ToString());
           sr.NextBytes(dataToSend);
  
           stream.Write(dataToSend, 0, dataToSend.Length);
  
           int totalRead = 0;
           string response = "";
           byte[] buff = new byte[1000];
           do
           {
                totalRead = stream.Read(buff, 0, buff.Length);
                response += Encoding.ASCII.GetString(buff, 0, totalRead);
           } while (totalRead == buff.Length);
       }
   } 
  
  class MyTlsClient : DefaultTlsClient
  {
      public override TlsAuthentication GetAuthentication() 
      {
          return new MyTlsAuthentication();
      }
  }
  
  class MyTlsAuthentication : TlsAuthentication
  {
      public TlsCredentials GetClientCredentials(CertificateRequest certificateRequest) { return null; }
  
      public void NotifyServerCertificate(Certificate serverCertificate) {    }
  }
  </code></pre>
  
  <p>What I've already done : </p>
  
  <ul>
  <li><p>Using WireShark to decrypt the ssl stream and inspect the request send => I've never succeeded to decrypt ssl stream</p></li>
  <li><p>Using fiddler to decrypt the https stream => No detection by fiddler so I suspect something might be badly encrypted </p></li>
  </ul>
  
  <p>Any ideas ?</p>