Making HTTPS call in C# with the BouncyCastle library

后端 未结 1 1783
自闭症患者
自闭症患者 2021-01-16 12:04

Using C# 4.0, I need to make HTTPS call with the BouncyCastle library (Short story : Windows XP + TLS 1.2).

When using the following code, I get a \"HTTP Err

1条回答
  •  被撕碎了的回忆
    2021-01-16 12:42

    Thanks to PeterDettman who gave me the solution :

    I must not use the sr.NextBytes(instructions), so the code becomes :

    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());
    
             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);
         }
     } 
    

    0 讨论(0)
提交回复
热议问题