0

HttpWebRequest 経由で SSL ページにアクセスしようとしています。このページは JavaScript を使用しているようです。ログインページを取得できますが、ログインした後 (ログインしていると思います)、次のページを取得できません。これが私のコードです:

private void fetch(String password)
    {
        try
        {
            ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);
            CookieContainer cookies = new CookieContainer();
            HttpWebRequest http = (HttpWebRequest)WebRequest.Create("https://facebook.com/");
            http.CookieContainer = cookies;
            http.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)";

            IWebProxy proxy = http.Proxy;
            if (proxy != null)
            {
                Console.WriteLine("Proxy: {0}", proxy.GetProxy(http.RequestUri));
            }
            else
            {
                Console.WriteLine("Proxy is null; no proxy will be used");
            }

            WebProxy myProxy = new WebProxy();
            Uri newUri = new Uri("myproxy");
            myProxy.Address = newUri;
            myProxy.Credentials = new NetworkCredential("username", password);
            http.Proxy = myProxy;

            HttpWebResponse response = (HttpWebResponse)http.GetResponse();           

            /////////////////////////////////////////////////////////
            HttpStatusCode responseStatus;
            responseStatus = response.StatusCode;

            if (responseStatus == HttpStatusCode.OK)
            {                
                UriBuilder urlBuilder = new UriBuilder("https://facebook.com/");

                ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true);                    
                HttpWebRequest request = (HttpWebRequest)WebRequest.Create(urlBuilder.ToString());
                request.CookieContainer = cookies;
                request.Referer = formUrl.ToString();
                request.Method = "POST";
                request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 6.0)";
                request.ContentType = "application/x-www-form-urlencoded";
                request.AllowAutoRedirect = false;                    

                using (Stream requestStream = request.GetRequestStream())
                using (StreamWriter requestWriter = new StreamWriter(requestStream, Encoding.ASCII))
                {
                    request.PreAuthenticate = true;
                    ICredentials credentials = new NetworkCredential("username", password);
                    request.Credentials = credentials;                        
                    request.Method = "POST";
                }
                string responseContent = null;

                using (HttpWebResponse response2 = (HttpWebResponse)request.GetResponse())
                {
                    using (Stream responseStream = response2.GetResponseStream())
                    {

                        using (StreamReader responseReader = new StreamReader(responseStream))
                        {
                            responseContent = responseReader.ReadToEnd();
                        }

                        Console.WriteLine(responseContent);
                        response.Close();
                        response2.Close();
                    }
                }
            }
            else
            {
                Console.WriteLine("Client was unable to connect!");
            }               
        }
        catch (UriFormatException e)
        {
            Console.WriteLine("Invalid URL");
        }
        catch (IOException e)
        {
            Console.WriteLine("Could not connect to URL");
        }            
    }
4

0 に答える 0