WebRequest and WebClient - Imitate an Ajax post in c#

admin

Administrator
Staff member
I was greeted with an interesting and strange issue today. a bit of history - A few days ago I wrote a method utilizing
Code:
Webclient
to post information to a wordpress login page, and it returned to me an HTML response which was either "Logged In" or "Invalid credentials"

Code:
...
                using (var webClient = new WebClient())
                {

                    var email = ((User)user).email;
                    var password = StringCipher.Decrypt(((User)user).password, CIFFConstants.CryptoKey);
                    webClient.UploadValues($"{host}/bloginternal/wp-login.php", new NameValueCollection()
                            {
                                { "log", email },
                                { "pwd", password
                            }});
                    var message = System.Text.Encoding.Default.GetString(result);
                    LogResponseFromServer(message);
                }...

This method went awry today by returning me the login page itself where it didn't seem to have posted the log and pwd variables to the form on the page, nor attempt to submit the form.

I thought "ok, maybe WebClient is not the best way to deal with this situation" and took a second approach using WebRequest

Code:
            var email = ((User) user) + "test";
            var password = StringCipher.Decrypt(((User) user).password, CIFFConstants.CryptoKey);

            var baseAddress = $"{host}/bloginternal/wp-login.php";

            var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
            http.Accept = "application/json";
            http.ContentType = "application/json";
            http.Method = "POST";

            var parsedContent = new JavaScriptSerializer().Serialize(new { log = email, pwd = password});
            var encoding = new ASCIIEncoding();
            var bytes = encoding.GetBytes(parsedContent);

            var newStream = http.GetRequestStream();
            newStream.Write(bytes, 0, bytes.Length);
            newStream.Close();

            var response = http.GetResponse();

            var stream = response.GetResponseStream();
            var sr = new StreamReader(stream);
            var content = sr.ReadToEnd();

note the bogus "test" I added after email - that was literally for testing purposes to see if the page would return "invalid login" to me like it used to.To my dismay, it still returned only the login page, where the form didn't seem like it submitted any information.

The ajax I am trying to reproduce is as follows:

Code:
$.ajax({
                url: '/bloginternal/wp-login.php',
                data: { log: values.Email, pwd: values.Password },
                type: "POST",
                async: false,
                success: function () {

                }
            });

The strange part is that the
Code:
WebClient
solution worked perfectly fine and I was able to imitate the ajax call listed above by checking whether the response returned a "logged in" status or "invalid credentials" status.

What could be the reasons why the site I am posting to returns only the stock login page as if no information has been submitted? Have I missed anything in my reproductions of the Ajax call which might have affected what the server returned to me?