使用BouncyCastle库在C#中进行HTTPS调用 [英] Making HTTPS call in C# with the BouncyCastle library

查看:424
本文介绍了使用BouncyCastle库在C#中进行HTTPS调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用C#4.0,我需要使用BouncyCastle库(简短的故事:Windows XP + TLS 1.2)进行HTTPS调用。

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

在使用以下代码时,我得到 HTTP错误400。请求动词无效。

When using the following code, I get a "HTTP Error 400. The request verb is invalid."

这是我的代码:

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) {    }
}

我已经完成的工作:


  • 使用WireShark解密ssl流并检查请求send => I' ve从未成功解密ssl流

  • Using WireShark to decrypt the ssl stream and inspect the request send => I've never succeeded to decrypt ssl stream

使用提琴手解密https流=>提琴手未检测到,因此我怀疑某些内容可能未正确加密

Using fiddler to decrypt the https stream => No detection by fiddler so I suspect something might be badly encrypted

有什么想法吗?

推荐答案

感谢PeterDettman给了我解决方案:

Thanks to PeterDettman who gave me the solution :

我一定不能使用sr.NextBytes(指令),所以代码变成:

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

这篇关于使用BouncyCastle库在C#中进行HTTPS调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆