解决了:
SslStream不会一次读取整个数据包...



我正在尝试使用一些准备好的数据包登录m.facebook.com。我用SslStream发送它。响应似乎还可以。但是它不包含必需的名为xs的cookie。我保证我准备好的小包有问题。但是我尝试使用Fiddler和SocketTest3发送相同的数据包,但我收到了cookie。可能是什么问题呢。

我使用的代码隐藏了登录名和密码。

   static void Main(string[] args)
    {
        SslTcpClient.TryLogin("m.facebook.com", "m.facebook.com");
        Console.ReadKey(true);
    }

   public class SslTcpClient
    {
        private static Hashtable certificateErrors = new Hashtable();

        public static bool ValidateServerCertificate(
              object sender,
              X509Certificate certificate,
              X509Chain chain,
              SslPolicyErrors sslPolicyErrors)
        {
            if (sslPolicyErrors == SslPolicyErrors.None)
                return true;

            Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

            return false;
        }
        public static void TryLogin(string machineName, string serverName)
        {
            TcpClient client = new TcpClient(machineName, 443);
            Console.WriteLine("Client connected.");
            SslStream sslStream = new SslStream(
                client.GetStream(),
                false,
                new RemoteCertificateValidationCallback(ValidateServerCertificate),
                null
                );
            try
            {
                sslStream.AuthenticateAsClient(serverName);
            }
            catch (AuthenticationException e)
            {
                Console.WriteLine("Exception: {0}", e.Message);
                if (e.InnerException != null)
                {
                    Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                }
                Console.WriteLine("Authentication failed - closing the connection.");
                client.Close();
                return;
            }
            var message = "POST https://m.facebook.com/login.php?refsrc=https%3A%2F%2Fm.facebook.com%2F&refid=8 HTTP/1.1\r\n" +
                    "Host: m.facebook.com\r\n" +
                    "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0\r\n" +
                    "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" +
                    "Accept-Language: pl,en-us;q=0.7,en;q=0.3\r\n" +
                    "Accept-Encoding: gzip, deflate\r\n" +
                    "Referer: https://m.facebook.com/\r\n" +
                    "Connection: keep-alive\r\n" +
                    "Content-Type: application/x-www-form-urlencoded\r\n" +
                    "Content-Length: 456\r\n\r\n" +
                    "lsd=AVqbr7Ql&charset_test=%E2%82%AC%2C%C2%B4%2C%E2%82%AC%2C%C2%B4%2C%E6%B0%B4%2C%D0%94%2C%D0%84&version=1&ajax=0&width=0&pxr=0&gps=0&m_ts=1388099668&li=VLi8UiFtZGcNu7Ux7Vt6X7UM&signup_layout=layout%7Cbottom_clean%7C%7Cwider_form%7C%7Cprmnt_btn%7Cspecial%7C%7Cst%7Ccreate%7C%7Cheader_crt_acct_button%7C%7Chdbtn_color%7Cgreen%7C%7Csignupinstr%7C%7Claunched_Mar3&email=LOGIN&pass=PASSWORD&login=Zaloguj+si%C4%99&_fb_noscript=true";

            byte[] messsage = Encoding.UTF8.GetBytes(message);
            sslStream.Write(messsage);
            sslStream.Flush();
            var buffer = new byte[10000];
            int r = sslStream.Read(buffer,0,buffer.Length);
            string serverMessage = Encoding.UTF8.GetString(buffer, 0, r);
            Console.WriteLine("Server says: {0}", serverMessage);
            client.Close();
            Console.WriteLine("Client closed.");
        }

    }

最佳答案

解决了:
SslStream不会一次读取整个标头。需要Double Read方法调用。
Facebook可能会单独发送标头的各个部分。

关于c# - C#SslStream是否不适合登录到Facebook?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20793324/

10-09 06:46