Introduction
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.
Background
You may want to understand PHP, or you could just write the backend in ASP.<stocktickeruk w:st="on"><stocktickerca w:st="on">NET which would also be easy. I chose not to do a web service simply to stay away from <stocktickeruk w:st="on"><stocktickerca w:st="on">SOAP 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;
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()
{
private static void WritePHPInfo(string message)
{
ASCIIEncoding encoding=new ASCIIEncoding();
string postData=DateTime.Now.ToShortTimeString();
byte[] data = encoding.GetBytes(postData);
HttpWebRequest myRequest;
try
{
myRequest = (HttpWebRequest)WebRequest.Create
("http://ipaq/oos.php?touch=" + message + " at:" +
DateTime.Now.ToShortTimeString() );
myRequest.Timeout=15000;
myRequest.Method = "POST";
myRequest.ContentType="application/x-www-form-urlencoded";
myRequest.ContentLength = data.Length;
Stream newStream=myRequest.GetRequestStream();
newStream.Write(data,0,data.Length);
newStream.Flush();
newStream.Close();
}
catch(WebException ex)
{
}
finally
{
myRequest=null;
}
}
Private void OK_click(Object sender, System.EventArgs e)
{
WritePHPInfo("Calling to Web method via PHP and HttpWebRequest");
}
<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";
/ file in this directory
$myFile= fopen($filename,'a');
if(! $myFile)
{
print ("File could not be opened.");
exit;
}
$string = "*****\n".$_REQUEST['touch']."\n*********\n";
fputs($myFile, $string);
fclose($myFile);
?>
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.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.