예제 #1
0
        // This is a TEST login function -- it will send the argument email and password combination to the login.php file on the Apache server.
        // That login.php file will return one of two associative arrays:
        // 1: A "login successful" array that will have the following key/value pairs (note: the format is "key" -> "value"):
        //      - "Status" -> "true" // This is a boolean in the PHP file but gets converted to a string when the LoginResponse is constructed.
        //      - "Message" -> "Login successful" // This is a string
        //      - "Email" -> *Email* // This the Email of the user as a string
        //
        // 2: A "login unsuccessful" array that will have the following key/value pairs:
        //      - "Status" -> "false" // This is a boolean in the PHP file but gets converted to a string when the LoginResponse is constructed.
        //      - "Message" -> "Login unsuccessful: invalid email and password combination" // This is a string
        //
        // These associative arrays are then converted into a LoginResponse object via a call to JsonConvert.DeserializeObject().
        // A LoginResponse object has get/set Status, Message, and Email functions -- these allow you to easily see if the attempted login was successful.
        public async Task <LoginResponse> login(string email, string password)
        {
            Debug.WriteLine("********** RestService.test_login() START **********");

            // Identify the target PHP file.
            var uri = new Uri("http://75.108.69.184:1337/server_side_files/application_files/login.php");

            try
            {
                // Create the content that will be posted to the target PHP file.
                FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("email", email),
                    new KeyValuePair <string, string>("password", password)
                });

                // Send the POST request to the PHP file at the specified URI.
                HttpResponseMessage response = await client.PostAsync(uri, formUrlEncodedContent);

                // Read the HttpResponseMessage's Content property as a string.
                string responseContent = await response.Content.ReadAsStringAsync();

                // Because the PHP file sends back a JSON encoded associative array, we can use the "Newtonsoft.Json" package to deserialize the JSON string and, thereby, create LoginResponse object.
                loginResponse = JsonConvert.DeserializeObject <LoginResponse>(responseContent);

                Debug.WriteLine("RestService.test_login(): RESPONSE CONTENT AS FOLLOWS:");

                // This will print the JSON string itself -- this is what is "deserialized" by the JsonConvert.DeserializeObject() function.
                Debug.WriteLine(responseContent);

                Debug.WriteLine("RestService.test_login(): RESPONSE CONTENT END");

                // Print the contents of the LoginResponse to see if the attempted login was successful.
                // In this case "successful" just means that the email/password combination given to this test_login() function was in the SQL database.
                Debug.WriteLine(loginResponse.GetContents());
                return(loginResponse);
            }

            catch (Exception e)
            {
                Debug.WriteLine(e);
                Debug.Fail("RestService.test_login(): something went wrong!");
                return(null);
            }
            //Debug.WriteLine("********** RestService.test_login() END **********");
        }
예제 #2
0
        //==============================================================================================================================================================================//

        // This is a test function. It is meant to connect to a PHP web service that remembers the username of the user who logs in (this is done via $_SESSION in the PHP file).
        // This function will perform four actions:
        // 1: it will attempt to login.
        // 2: it will attempt to get request data -- this should only succeed if the previous login attempt succeeded.
        // 3: it will attempt to logout.
        // 4: it will attempt to get request data again -- this should fail provided the previous logout was successful.
        public async void test_Login_GetRequestData_Logout_GetRequestData_WithSession()
        {
            Debug.WriteLine("********** RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession() START **********");

            // The user to login as.
            string email    = "*****@*****.**";
            string password = "******";

            // Get at most two rows from the database.
            int maxNumRowsToGet = 2;

            // Skip the first two rows.
            int startRowOffset = 2;

            //----------------------------------------------------------------------------------------------------------//
            // Attempt login.

            var uri = new Uri("http://75.108.69.184:1337/server_side_files/application_files/login.php");

            try
            {
                FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("email", email),
                    new KeyValuePair <string, string>("password", password)
                });

                HttpResponseMessage response = await client.PostAsync(uri, formUrlEncodedContent);

                string responseContent = await response.Content.ReadAsStringAsync();

                Debug.WriteLine("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): LOGIN: RESPONSE CONTENT AS FOLLOWS:");

                Debug.WriteLine(responseContent);

                Debug.WriteLine("-----------------------------------------------------------------------------------------");

                try
                {
                    LoginResponse loginResponse = JsonConvert.DeserializeObject <LoginResponse>(responseContent);

                    Debug.WriteLine(loginResponse.GetContents());
                }

                catch (Exception e)
                {
                    Debug.Fail("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): LOGIN: something went wrong while deserializing \"responseContent\":");
                    Debug.WriteLine(e);
                }

                Debug.WriteLine("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): LOGIN: RESPONSE CONTENT END");
            }

            catch (Exception e)
            {
                Debug.WriteLine(e);
                Debug.Fail("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): LOGIN: something went wrong while logging in!");
            }

            //----------------------------------------------------------------------------------------------------------//
            // Attempt to get request data.

            uri = new Uri("http://75.108.69.184:1337/server_side_files/get_request_data.php");

            try
            {
                FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("maxNumRowsToGet", maxNumRowsToGet.ToString()),
                    new KeyValuePair <string, string>("startRowOffset", startRowOffset.ToString())
                });

                HttpResponseMessage response = await client.PostAsync(uri, formUrlEncodedContent);

                string responseContent = await response.Content.ReadAsStringAsync();

                Debug.WriteLine("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): GET REQUEST DATA: RESPONSE CONTENT AS FOLLOWS:");

                Debug.WriteLine(responseContent);

                Debug.WriteLine("-----------------------------------------------------------------------------------------");

                try
                {
                    List <RequestViewModel> rvmList = JsonConvert.DeserializeObject <List <RequestViewModel> >(responseContent);

                    foreach (RequestViewModel rvm in rvmList)
                    {
                        Debug.WriteLine(rvm.GetContents());
                    }
                }

                catch (Exception e)
                {
                    Debug.Fail("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): GET REQUEST DATA: something went wrong while deserializing \"responseContent\":");
                    Debug.WriteLine(e);
                }

                Debug.WriteLine("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): GET REQUEST DATA: RESPONSE CONTENT END");
            }

            catch (Exception e)
            {
                Debug.WriteLine(e);
                Debug.Fail("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): GET REQUEST DATA: something went wrong while trying to get request data!");
            }

            //----------------------------------------------------------------------------------------------------------//
            // Attempt to logout.

            uri = new Uri("http://75.108.69.184:1337/server_side_files/application_files/logout.php");

            try
            {
                HttpResponseMessage response = await client.GetAsync(uri);

                string responseContent = await response.Content.ReadAsStringAsync();

                Debug.WriteLine("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): LOGOUT: RESPONSE CONTENT AS FOLLOWS:");
                Debug.WriteLine(responseContent);
                Debug.WriteLine("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): LOGOUT: RESPONSE CONTENT END");
            }

            catch (Exception e)
            {
                Debug.WriteLine(e);
                Debug.Fail("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): LOGOUT: something went wrong while attempting to logout!");
            }

            //----------------------------------------------------------------------------------------------------------//
            // Attempt to get request data again. This should fail since we've already logged out.

            uri = new Uri("http://75.108.69.184:1337/server_side_files/application_files/get_request_data.php");

            try
            {
                FormUrlEncodedContent formUrlEncodedContent = new FormUrlEncodedContent(new[]
                {
                    new KeyValuePair <string, string>("maxNumRowsToGet", maxNumRowsToGet.ToString()),
                    new KeyValuePair <string, string>("startRowOffset", startRowOffset.ToString())
                });

                HttpResponseMessage response = await client.PostAsync(uri, formUrlEncodedContent);

                string responseContent = await response.Content.ReadAsStringAsync();

                Debug.WriteLine("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): GET REQUEST DATA: RESPONSE CONTENT AS FOLLOWS:");

                Debug.WriteLine(responseContent);

                Debug.WriteLine("-----------------------------------------------------------------------------------------");

                try
                {
                    List <RequestViewModel> rvmList = JsonConvert.DeserializeObject <List <RequestViewModel> >(responseContent);

                    foreach (RequestViewModel rvm in rvmList)
                    {
                        Debug.WriteLine(rvm.GetContents());
                    }
                }

                catch (Exception e)
                {
                    Debug.Fail("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): GET REQUEST DATA: something went wrong while deserializing \"responseContent\":");
                    Debug.WriteLine(e);
                }

                Debug.WriteLine("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): GET REQUEST DATA: RESPONSE CONTENT END");
            }

            catch (Exception e)
            {
                Debug.WriteLine(e);
                Debug.Fail("RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession(): GET REQUEST DATA: something went wrong while trying to get request data!");
            }

            //----------------------------------------------------------------------------------------------------------//

            Debug.WriteLine("********** RestService.test_Login_GetRequestData_Logout_GetRequestData_WithSession() END **********");
        }