【问题标题】:wrap in ssl stream with sslStream class使用 sslStream 类包装在 ssl 流中
【发布时间】:2013-09-04 15:52:06
【问题描述】:

当我运行下面的代码时,会发生错误

    string host = "www.google.com";
    int proxyPort = 443;//443;

    byte[] buffer = new byte[2048];
    int bytes;

    // Connect socket
    TcpClient client = new TcpClient(host, proxyPort);
    NetworkStream stream = client.GetStream();

    // Establish Tcp tunnel
    byte[] tunnelRequest = Encoding.UTF8.GetBytes(String.Format("CONNECT {0}:443  HTTP/1.1\r\nHost: {0}\r\n\r\n", host));
    stream.Write(tunnelRequest, 0, tunnelRequest.Length);
    stream.Flush();

    // Read response to CONNECT request
    // There should be loop that reads multiple packets
    bytes = stream.Read(buffer, 0, buffer.Length);
    Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));

    // Wrap in SSL stream
    SslStream sslStream = new SslStream(stream);
    sslStream.AuthenticateAsClient(host);

错误发生在这一行

    sslStream.AuthenticateAsClient(host);

错误是:无法将数据写入传输连接:已建立的连接已被主机中的软件中止。

我该如何解决这个问题?谢谢

【问题讨论】:

    标签: sockets ssl tcp https sslstream


    【解决方案1】:

    您正在向 HTTPS 服务器发送 CONNECT,该服务器正确地关闭了连接,因为它不是有效的 HTTPS 握手。

    CONNECT 仅发送到上游代理(为了通过代理建立隧道);它从不发送到源服务器本身。即使您使用的是代理,您读取 CONNECT 响应的代码也有问题,而且不太可能正常工作。

    你有没有考虑让我为你做艰苦的工作? http://fiddler2.com/core/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      相关资源
      最近更新 更多