我正在尝试通过使用SSL和自签名证书到HTTPS站点来发出GET和POST请求,我希望我的程序从浏览器获取GET和POST请求并使用自签名证书打开此SSL,实际上我想要HTML https网站的源代码,例如我使用了twitter:
static void Main(string[] args)
{
string host = "twitter.com";
// Connect socket
TcpClient client = new TcpClient(host, 443);
NetworkStream stream = client.GetStream();
// Wrap in SSL stream
SslStream sslStream = new SslStream(stream, false, new RemoteCertificateValidationCallback(ValidateServerCertificate));
sslStream.AuthenticateAsClient(host);
//Build request
var sb = new StringBuilder();
sb.AppendLine("GET / HTTP/1.1");
sb.AppendLine(string.Format("Host: {0}", host));
sb.AppendLine("Connection: keep-alive");
sb.AppendLine("User-Agent: CSharp");
sb.AppendLine("Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
sb.AppendLine("Accept-Encoding: gzip,deflate,sdch");
sb.AppendLine("Accept-Language: en-US,en;q=0.8");
sb.AppendLine("Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3");
sb.AppendLine();
//Go go go!
byte[] headerBytes = Encoding.UTF8.GetBytes(sb.ToString());
sslStream.Write(headerBytes, 0, headerBytes.Length);
sslStream.Flush();
//Get a place to put it
byte[] buffer = new byte[2048];
int bytes;
//Answer
do
{
bytes = sslStream.Read(buffer, 0, buffer.Length);
Console.Write(Encoding.UTF8.GetString(buffer, 0, bytes));
} while (bytes != 0);
//And done
client.Close();
Console.ReadKey();
}
public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
return true;
}
我到控制台的反应是:
HTTP/1.1 307 Temporary Redirect
cache-control: no-cache
content-length: 0
date: Sun, 26 Jun 2016 15:11:54 GMT
location: https://mobile.twitter.com/
server: tsa_f
set-cookie: guest_id=v1%3A146695391459213782; Domain=.twitter.com; Path=/; Expires=Tue, 26-Jun-2018 15:11:54 UTC
status: 307 Temporary Redirect
strict-transport-security: max-age=631138519
x-connection-hash: b644a95971e954e39bc0f563f88d51d6
x-content-type-options: nosniff
x-frame-options: SAMEORIGIN
x-response-time: 93
x-transaction: 000570e300d30436
x-xss-protection: 1; mode=block
如何使用SSL和自签名证书获取HTTPS站点的HTML源代码?