Windows MobileVisual Studio .NET 2003Windows 2003Windows 2000Windows XPPHP中级开发Visual StudioWindowsC++.NETC#
Compact Framework - PHP 写入器
使用 PDA 写入 PHP 服务器
引言
我最近在一个项目中工作,我的设备需要唤醒并运行一些例程,而且我还想确保应用程序不在某些时间运行。 我真的不想坐在那里读取设备上的本地日志文件,所以我创建了一个快速例程,将我需要的信息记录到我的 Apache 服务器。 一点 PHP,一点 C#,耶! 我们成功了。
背景
您可能需要理解 PHP,或者您可以只用 ASP 编写后端。
构建代码
首先启动 VS.NET 2003 并创建一个新项目
在本教程中,我将我的应用程序命名为 PHPApp。 选择“确定”,系统将提示您选择此应用程序的 exe 文件的选项对话框,我们将选择 PocketPC – Windows 应用程序。
选择“确定”。 然后您将在设计器中看到一个空白表单。
现在在表单上放置一个按钮 - 在代码中以下列出为 OK。 现在选择按钮的“名称”属性并将其更改为 OK。
表单现在包含一个按钮
双击该按钮,并添加下面列出的按钮事件的代码以及用于将数据写入 PHP 网页的静态对象和事件。 我在按钮事件中放置了静态文本,但您可以轻松地将其更改为您想要的任何内容。
使用代码
有多种使用此代码的方法。 您可以只在一个事件(例如按钮单击)上调用所有代码,或者您可以设置一个受保护的方法,例如我在这里描述的方法。 然后,我为该项目设置了一个静态变量来保存状态,我从一个 xml 配置文件中提取了该状态,本文不会涉及该配置文件,但将在后续文章中进行讨论。
请务必使用
- using System.Net;
- using System.IO;
- using System.Text;
//I used a static variable so that in my other methods I could simply //do and if statement so I’m going to list it here. //I’m going to setup the IsPHPEnable member variable to true to be sure it //uses my method for logging. using System.Net; using System.IO; using System.Text; Namespace PHPApp { public class Form1: System.Windows.Forms.Form { private static bool IsPHPEnabled=false; public Form1() { /*Ok, now that we have the variable setup, lets move on to setting up the Method itself that we’ll use when we want to log info to the Web Page I call it WritePHPInfo and it takes a string for the message */ private static void WritePHPInfo(string message) { ASCIIEncoding encoding=new ASCIIEncoding(); string postData=DateTime.Now.ToShortTimeString(); byte[] data = encoding.GetBytes(postData); HttpWebRequest myRequest; // Prepare web request... try { myRequest = (HttpWebRequest)WebRequest.Create ("http://ipaq/oos.php?touch=" + message + " at:" + DateTime.Now.ToShortTimeString() ); //************note: I’m passing the call to ?touch, you’ll see //below in the php where touch is used for input myRequest.Timeout=15000; myRequest.Method = "POST"; myRequest.ContentType="application/x-www-form-urlencoded"; myRequest.ContentLength = data.Length; Stream newStream=myRequest.GetRequestStream(); // Send the data. newStream.Write(data,0,data.Length); newStream.Flush(); newStream.Close(); } catch(WebException ex) { //be sure you look in here I’m not using it for this example } finally { myRequest=null; } } /* Now lets look at how we can now call it, I’ll create a sample function, lets say that were going to call it from a button on click event, so just place a button on your app and call it OK for it’s name, go to the Action Page and double click tThe on click event */ Private void OK_click(Object sender, System.EventArgs e) { // Now lets fire off our method WritePHPInfo(“Calling to Web method via PHP and HttpWebRequest”); } //**PHP PORTION LOCATED ON APACHE SERVER** /*Ok, so you can run PHP on Windows or Linux as well as Apache. I’m not going to get into how to configure PHP to work with Windows but be assured it works just fine. HINT: be sure you have php.exe setup for php extensions in IIS. I chose to use Php on my Linux RedHat 9 machine so my path will be associated with that. On my system Apache is setup to look at /var/www/html as it root location for serving up pages. IIS would be under \InetPub\wwwroot\. I then proceeded to create a quick PHP page.. Granted to be to hard on me with the PHP I just threw it up and it has very limited Exception handlers. I call it logwriter.php I also created a form so I could test it from the actual HTML gui. */ <html> <body> A Simple Form <br><br> <form action="logwriter.php" method="post"> <input type="text" name="touch" size="10"> <br><br> <input type="submit" name="submitbutton"> </form> </body> </html> <?php $filename ="/tmp/data.txt"; // NOTICE: I’m writing to /tmp/ because apache as rights to write to the / file in this directory // First assign a name (and also a path if the file will not be in the // same directory) to the file that we're going to create. $myFile= fopen($filename,'a'); // Open the file for writing if(! $myFile) { // Make sure the file was opened successfully print ("File could not be opened."); exit; } //**note: here is the ‘touch’ we talked about in the .Net code. ** // Create data to be added to the text file. ********************* $string = "*****\n".$_REQUEST['touch']."\n*********\n"; //************************************************************ fputs($myFile, $string); // Write the data ($string) to the text file fclose($myFile); // Closing the file after writing data to it ?>
关注点
这不太复杂,只需几分钟就可以完成,并且节省了我站在设备前的一些时间。 我希望您能够在您的下一个项目中使用它。