我想使用Socket将数据发送到Web浏览器。 我使用SSlStream和AuthenticateAsServer函数,并向该函数发送许多证书,但是只要程序收到AuthenticateAsServer()行,该程序就会无限期地被阻塞。
为什么?请解释。 如何解决它或忽略身份验证?
A = ClientSocket.EndAccept(ar);
int ReciedCount = A.Receive(RawByteArrayData, SocketFlags.None);
string RawStringData = Encoding.ASCII.GetString(RawByteArrayData);
string[] LinesData = RawStringData.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
string Host = "";
for (int i = 1; i < LinesData.Length; i++)
{
if (LinesData[i].Contains("Host"))
{
Host = LinesData[i].Replace("Host: ","");
}
}
string[] HttpImprtantData = LinesData[0].Split(' ');
IPHostEntry HostEntry = null;
int Port = 0;
ServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
byte[] NewData = new byte[1024 * 10];
byte[] Body = new byte[1024 * 100];
byte[] An = new byte[1024 * 100];
HostEntry = Dns.GetHostEntry(Host);
switch (HttpImprtantData[0])//Method
{
case "CONNECT":
{
X509Store store = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
store.Open(OpenFlags.ReadOnly);
RawStringData = RawStringData.Replace("CONNECT ", "GET https://");
RawStringData = RawStringData.Replace(":443", "/");
RawStringData = RawStringData.Replace("Proxy-Connection", "Connection");
RawByteArrayData = Encoding.ASCII.GetBytes(RawStringData);
Port = 443;
ServerSocket.Connect(HostEntry.AddressList[0], Port);
HttpsStream = new SslStream(new NetworkStream(ServerSocket));
HttpsStream.AuthenticateAsClient(Host);
HttpsStream.Write(RawByteArrayData);
int Count = HttpsStream.Read(NewData, 0, NewData.Length);
string pfxpath = @"D:\test.pfx";
X509Certificate2 cert = new X509Certificate2(File.ReadAllBytes(pfxpath));
byte[] pfxData = File.ReadAllBytes(pfxpath);
cert = new X509Certificate2(pfxData,"", X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.Exportable);
X509Certificate x509 = store.Certificates.Find(X509FindType.FindByIssuerName, "localhost", false)[0];
ssl = new SslStream(new NetworkStream(A) , false , Verification , null );
ssl.AuthenticateAsServer(cert , false, System.Security.Authentication.SslProtocols.Tls, true);
ssl.Write(NewData);
};break;