繁体   English   中英

C#PHP通讯

[英]C# PHP communication

我正在编写一个应用程序,它将通过MySQL数据库对用户进行身份验证。 我已经用Java(android)编写了它,但是现在正在移植到Windows Phone。

PHP文件使用$ get,然后回显响应:

$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db($database_localhost, $localhost);

$username = $_POST['username'];

$query_search = "select * from users where user = '".$username."'";
//$query_search = "select * from users where username = '".$username."' AND password     = '".$password. "'";

$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);
//echo $rows;
if($rows == 0) {
echo "No Such User Found";
} else  {
    echo "User Found";
}

如何将username变量传递给PHP,然后接收结果?

您的代码很容易受到SQL注入方法的影响,请使用PDO / MYSQLi避免这种情况

创建加载的事件处理程序:

using System;
public MainPage()
{
InitializeComponent();

Loaded += new RoutedEventHandler(MainPage_Loaded);
}



void MainPage_Loaded(object sender, RoutedEventArgs e)
{
System.Uri myUri = new System.Uri("Your php page url");
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(myUri);
myRequest.Method = "POST";
myRequest.ContentType = "application/x-www-form-urlencoded";
myRequest.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback),myRequest);
}

创建“ POST”数据流:

void GetRequestStreamCallback(IAsyncResult callbackResult)
{
HttpWebRequest myRequest = (HttpWebRequest)callbackResult.AsyncState;
// End the stream request operation
Stream postStream = myRequest.EndGetRequestStream(callbackResult);

// Create the post data
string postData = "username=value";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

// Add the post data to the web request
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();

// Start the web request
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
        Debug.WriteLine(result);
    }
}

使用诸如我的服务器中有脚本之类的内部链接内容,您只需编写:“ example.com/save.php?username=textbox1.text&score=points”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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