我无法使用httpwebrequest登录网站

时间:2013-10-14 09:21:24

标签: windows-phone-8

我正在尝试使用POST的httpwebrequest登录网站(例如:https://www.facebook.com/login.php?login_attempt=1)。但登录后不会返回结果。

我不知道我的代码有什么问题,你能帮帮我吗?非常感谢!

这是我的代码:

        private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        System.Uri myUri = new System.Uri("https://www.facebook.com/login.php?login_attempt=1");
        HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
        myRequest.Method = "POST";
        myRequest.ContentType = "application/x-www-form-urlencoded";
        myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), myRequest);

    }

    void GetRequestStreamCallback(IAsyncResult callbackResult)
    {
            HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
            // End the stream request operation
            Stream postStream = myRequest.EndGetRequestStream(callbackResult);
            StringBuilder postData = new StringBuilder();
            postData.Append("email=myEmail");
            postData.Append("&password=myPassword");
            byte[] byteArray = Encoding.UTF8.GetBytes(postData.ToString());
            postStream.Write(byteArray, 0, postData.Length);
            postStream.Close();
            myRequest.BeginGetResponse(new AsyncCallback(GetResponsetStreamCallback), myRequest);
    }
    void GetResponsetStreamCallback(IAsyncResult callbackResult)
    {
            HttpWebRequest request = (HttpWebRequest)callbackResult.AsyncState;
            HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(callbackResult);
            using (StreamReader httpWebStreamReader = new StreamReader(response.GetResponseStream()))
            {
                string result = httpWebStreamReader.ReadToEnd();
                //For debug: show results
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                   // something do
                });
            }

    }

1 个答案:

答案 0 :(得分:0)

我只是快速查看:https://www.facebook.com/login.php的输入字段命名为"电子邮件"并且"通过" (不是"密码")。 未经测试:更改

postData.Append("&password=myPassword");

postData.Append("&pass=myPassword");

即使这样可行,但我怀疑你无论做什么都会进一步发展。我建议使用其中一个Facebook SDK。