0

我正在尝试通过 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
                });
            }

    }
4

1 回答 1

0

我只是快速浏览了一下:https ://www.facebook.com/login.php的输入字段被命名为“email”和“pass”(不是“password”)。未经测试:更改

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

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

即使这行得通,我怀疑你是否能在你打算做的任何事情上走得更远。我建议使用其中一种 Facebook SDK。

于 2013-10-15T07:47:47.970 回答