简体   繁体   中英

C# interacting with PHP using Visual Studio 2013 and making Windows 8.1 Universal App

Ok. So this question might be a bit stupid but as I've searched and searched and haven't found a working solution I thought I might ask here.

I've put up a simple PHP page that gets 2 parameters from the url myusername and mypassword. Then gets 3 int values from a database according to the username and password given.

(tested it By typing the url in with the parameters and the PHP script itself works. Even made it to echo the 3 integers on the page to make sure)

Now to the problematic part. I'm using Visual Studio 2013 and making an Universal App for Windows 8.1. And I just can't seem to get the httpClient to get me any data from there. Through browsing the forums I haven't been able to find anything that works. Couldn't have tested all either as most use GetResponse() which doesn't work in VS 2013. As I'm fairly new to the C# coding it could be as simple as to a little mistake in the dozens of tests I've done.

Made a login screen with 2 text fields. And I can build the url in form of "www.something.com/something/somephp?myusername=UserNameGiven&mypassword=PasswordGiven"

If anyone could give a simple solution on how I might be able to get the results from the page that address opens (only shows the 3 integers through echo... can remove those too if those arent required for the C# code to work. String format would probably be ideal if not too much to ask...

Ok made a new GetScores async method for the code you gave SnyderCoder. Throwing that Task on login button would have required coding beyond my knowhow for the moment atleast.

Still Results.Text field remains at the default status and shows no change.

Changed the LoginButton back to async without task.

the state of my code from the c# is atm is

private async void LoginButton_Click(object sender, RoutedEventArgs e)
{
   string UserName, Password;
   UserName = UserNameFeedField.Text.ToString();
   Password = PasswordFeedField.Text.ToString();
   string url = "something.com/something/GetScores.php?myusername=" + UserName + "&mypassword=" + Password;
   URL.Text = url;
   // GetScores(url);
   using (Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient())
   {
      string contentOFPage = await client.GetStringAsync(new Uri(url));

      Results.Text = contentOFPage;
   }
}

incase it matters here is the PHP code portion

<?php

$host="db_host"; // Host name 
$username="db_user"; // Mysql username 
$password="db_pswd"; // Mysql password 
$db_name="db_name"; // Database name 
$tbl_name="db_table"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// username and password sent from form 
$myusername=$_GET["myusername"]; 
$mypassword=$_GET["mypassword"]; 

// To protect MySQL injection (more detail about MySQL injection )
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql="SELECT field1 as LowB, field2 as LongB, field3 as Bs FROM $tbl_name WHERE UserID='$myusername' and UserPSWD='$mypassword'";
$result=mysql_query($sql);

// this part only shows the variables gotten from the query and echoes those on the page
// was only made to test that the PHP works.
while ($row = mysql_fetch_assoc($result)) {
    echo ($row["LowB"] . " " . $row["LongB"] . " " . $row["Bs"]);
}
?>

First a async method always needs to return a task. For the rest:

private async void LoginButton_Click(object sender, RoutedEventArgs e)
{
    string UserName = UserNameFeedField.Text.ToString();
    string Password = PasswordFeedField.Text.ToString();
    string url = "www.something.com/something/some.php?myusername=" + UserName + "&mypassword=" + Password;

    using (Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient())
    {
        string contentOfPage = await client.GetStringAsync(new Uri(url));

        //Do something with the contentOfPage
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM