当我运行下面的代码时,出现错误
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.1rnHost: {0}rnrn", 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);
错误为:无法将数据写入传输连接:主机中的软件中止了已建立的连接。
我该如何解决这个问题?感谢
您正在向HTTPS服务器发送CONNECT,该服务器正确地关闭了连接,因为它不是有效的HTTPS握手。
CONNECT仅发送到上游代理(以便通过代理进行隧道传输);它从不发送到原始服务器本身。即使您使用代理,读取CONNECT响应的代码也是有缺陷的,不太可能工作。
你有没有考虑过让我为你做艰苦的工作?http://fiddler2.com/core/