dousong2023 2014-07-14 10:10
浏览 61

C#使用Visual Studio 2013与PHP交互并制作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"]);
}
?>
  • 写回答

1条回答 默认 最新

  • douba4933 2014-07-14 10:18
    关注

    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
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序