Http CONNECT请求返回空的respone

时间:2016-02-28 20:08:29

标签: c# https windows-8.1 connect tcpclient

我想使用TcpClient阅读Https页面。我使用下面的代码

var client = new TcpClient(url, 443);//"127.0.0.1", 8888);// Fiddler port
client.SendTimeout = 30000;
Stream responseStream = client.GetStream();

// send CONNECT request to server
byte[] tunnelRequest = Encoding.ASCII.GetBytes("CONNECT www.google.com:443 HTTP/1.1\r\nUser-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:44.0) Gecko/20100101 Firefox/35.0\r\nProxy-Connection: keep-alive\r\nConnection: keep-alive\r\nHost: www.google.com:443\r\n\r\n");
responseStream.Write(tunnelRequest, 0, tunnelRequest.Length);
responseStream.Flush();

// read CONNECT response 
string connectResponse = ReadResponse(responseStream);
Console.WriteLine("server connect response :  " + connectResponse);

向主持人发送CONNECT个请求(google.com)

CONNECT www.google.com:443 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0
Proxy-Connection: keep-alive
Connection: keep-alive
Host: www.google.com:443

respone必须是这样的事情

HTTP/1.1 200 Connection Established
StartTime: 22:42:38.774
Connection: close

responseStream什么也没有回复。当我使用Fiddler作为代理时

var client = new TcpClient("127.0.0.1", 8888);

它工作正常并返回200响应。 Fiddler修复它有什么不对吗?

我使用Windows 8.1并使用.Net 2和4.5.1进行测试。

1 个答案:

答案 0 :(得分:0)

基于代理存在时使用的rfc CONNECT方法。

  

因为TLS特别需要提供端到端的连接   验证和防止中间人攻击,这个备忘录   指定在代理之间建立隧道的CONNECT方法。

如果是直接沟通:

var client = new TcpClient("127.0.0.1", 8888);//url, 443);//
client.SendTimeout = 30000;
Stream responseStream = client.GetStream();

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

byte[] byts = Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\nHost: www.google.com\r\nConnection: keep-alive\r\n\r\n");
sslStream.Write(byts, 0, byts.Length);

var str = ReadResponse(sslStream);