C# tcp模擬請求https

 1             var host = new Uri("https://www.jiecaopay.com");
 2             var client = new TcpClient(host.Host, host.Port);
 3 
 4             var sslStream = new SslStream(client.GetStream(), true
 5                     , new RemoteCertificateValidationCallback((sender, certificate, chain, sslPolicyErrors)
 6                         =>
 7                     {
 8                         return sslPolicyErrors == SslPolicyErrors.None;
 9                     }
10                         ), null);
11             sslStream.ReadTimeout = TimeOut * 1000;
12             sslStream.WriteTimeout = TimeOut * 1000;
13 
14             X509Store store = new X509Store(StoreName.My);
15 
16             //sslStream.AuthenticateAsClient(host.Host, store.Certificates, System.Security.Authentication.SslProtocols.Default, false);
17 
18             sslStream.AuthenticateAsServer(cert, true, SslProtocols.Tls, true);
19 
20             var send = GetSendHeaders(host, "", "");
21 
22             if (sslStream.IsAuthenticated)
23             {
24                 sslStream.Write(send, 0, send.Length);
25                 sslStream.Flush();
26 
27                 byte[] date = new byte[65535];
28                 while (true)
29                 {
30                     int len = sslStream.Read(date, 0, 65535);
31                     Console.WriteLine(Encoding.UTF8.GetString(date, 0, len));
32                 }
33             }

GetHeads方法是http頭html

 1         /// <summary>
 2         /// 返回請求的頭部內容
 3         /// </summary>
 4         /// <param name="uri">請求絕對地址</param>
 5         /// <param name="referer">請求來源地址,可爲空</param>
 6         /// <param name="postData">post請求參數. 設置空值爲get方式請求</param>
 7         /// <returns>請求頭部數據</returns>
 8         private static byte[] GetSendHeaders(Uri uri, string referer, string postData)
 9         {
10             string sendString = @"{0} {1} HTTP/1.1
11 Accept: text/html, application/xhtml+xml, */*
12 Referer: {2}
13 Accept-Language: zh-CN
14 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.72 Safari/537.36 Gecko/20100101 Firefox/47.0
15 Accept-Encoding: gzip, deflate
16 Host: {3}
17 Connection: keep-alive
18 Cache-Control: no-cache
19 ";
20 
21             sendString = string.Format(sendString, string.IsNullOrWhiteSpace(postData) ? "GET" : "POST", uri.PathAndQuery
22                 , string.IsNullOrWhiteSpace(referer) ? uri.AbsoluteUri : referer, uri.Host);
23             
24             if (string.IsNullOrWhiteSpace(postData))
25             {
26                 sendString += "\r\n";
27             }
28             else
29             {
30                 int dlength = Encoding.UTF8.GetBytes(postData).Length;
31 
32                 sendString += string.Format(@"Content-Type: application/x-www-form-urlencoded; charset=UTF-8
33 Content-Length: {0}
34 
35 {1}", postData.Length, postData);
36 
37             }
38 
39             return Encoding.UTF8.GetBytes(sendString);
40         }
相關文章
相關標籤/搜索