I was recently working on a project where my device needed to wake up and run some routines, I also needed to be sure that app did not run at certain times. I really didn’t want to sit around to read local log files on the device so I created a quick routine that would log the info I needed to my Apache server. A little PHP, a little C# and yee-haw we're there.
You may want to understand PHP, or you could just write the backend in ASP. which would also be easy. I chose not to do a web service simply to stay away from overhead. That could be conceived as good or bad depending on your viewpoint, it was just my opinion and I mainly wanted to see if it was really simple to do.
Building the Code
To start with fire up VS.NET 2003 and create a new project:
I’ve called my app PHPApp for this tutorial. Select OK and you well be prompted with an option dialog for the exe for this app we’ll select PocketPC – Windows Application.
Select OK. And you will have a blank form in the designer.
Now Drop a button on the Form – listed below in the code as OK. Now select the Name property of the button and change it to OK.
The Form now Contains a Button:
Double Click the Button and add the code listed below for the button event as well as the static object and the Event for writing data to the PHP web page. I’ve put static text in the button event, but you could easily change it to anything you want.
Using the code
There are a number of ways to use this code. You could just call all of it on a event such as a button click, or you could setup a protected method such as I’ll describe here. I then setup a static variable to the project to hold the state which I pulled from and xml config file which I won’t get into in this article but will address in a following one.
Be sure to use:
- using System.Net;
- using System.IO;
- using System.Text;
public class Form1: System.Windows.Forms.Form
private static bool IsPHPEnabled=false;
private static void WritePHPInfo(string message)
ASCIIEncoding encoding=new ASCIIEncoding();
byte data = encoding.GetBytes(postData);
myRequest = (HttpWebRequest)WebRequest.Create
("http://ipaq/oos.php?touch=" + message + " at:" +
myRequest.Method = "POST";
myRequest.ContentLength = data.Length;
Private void OK_click(Object sender, System.EventArgs e)
WritePHPInfo(“Calling to Web method via PHP and HttpWebRequest”);
A Simple Form
<form action="logwriter.php" method="post">
<input type="text" name="touch" size="10">
<input type="submit" name="submitbutton">
Points of Interest
This wasn’t overly complex and only took a few minutes to bang out and saved me a little time standing in front of the device. I hope you might be able to use it in your next project.