2016-02-28 214 views
0

我想使用TcpClient閱讀Https頁面。我用下面的代碼Http CONNECT請求返回空響應

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 

必須輸入反應不會停這樣

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

responseStream任何回報。當我使用提琴手作爲代理

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

它工作正常,並返回200響應。小提琴手修理它有什麼不對嗎?

我使用Windows 8.1並使用.Net 2和4.5.1進行測試。

回答

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