I am building a C#
application that needs to call out to a web page (in PHP
) to request that validation be performed against data in a database. Data is supplied to the PHP
page via HTTP parameters in the URL
.
I would like to retrieve a single response value back from a call to my PHP web page. In this specific example, I only need a Boolean
value. However, it seems prudent that I learn how to request anything, perhaps even multiple values in one request (if that's even possible).
This is a simplified version of the PHP page I am making the call to:
<?php
$type = $_GET['type'];
$accessid = $_GET['accessid'];
$license = $_GET['license'];
$machine = $_GET['machine'];
$osver = $_GET['osver'];
$ip = getenv("REMOTE_ADDR");
$query = "select * from validatetable where licnum = '" . $license . "'";
if ($result = db_doquery($query))
{
if (db_dofetcharray($result))
{
$query = "update validatetable set lastdate = CURRENT_TIMESTAMP, lastmachine = '" . $machine . "', accessip = '" . $ip . "', osver = '" . $osver . "' where type = '" . $type . "' and licnum = '" . $license . "'";
db_doquery($query);
}
else
{
$query = "insert into validatetable set type = '" . $type . "', name = '<unknown>', licnum = '" . $license . "', accessid = '" . $accessid . "', lastdate = CURRENT_TIMESTAMP, machine = '" . $machine . "', accessip = '" . $ip . "', osver = '" . $osver . "'";
db_doquery($query);
}
}
?>
This PHP
page is simply receiving values and either inserting or updating a record in a database table. This will continue, but there's another table from which I would like to extract information, compare to what was supplied, and return a validation indicator as Boolean
.
And this is my current request code in the C# application:
String webUrl = String.Format("http://www.mywebsite.com/validate.php?type=type&accessid={0}&license={1}&machine={2}&osver={3}.{4}", accessID, licNum, clientMachineName, Environment.OSVersion.Version.Major, Environment.OSVersion.Version.Minor);
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(webUrl);
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (response.StatusCode == HttpStatusCode.OK)
{
using (Stream dataStream = response.GetResponseStream())
{
using (StreamReader reader = new StreamReader(dataStream, Encoding.UTF8))
{
String theResponse = reader.ReadToEnd(); //<--- THIS LINE
}
}
}
response.Close();
The noted line results in the variable containing the full HTML of the page being returned.
What I would like to know is how I can get the response back as one or more of these (whatever is possible):
- A single value
- A collection of values
- Something from which I can reliably extract a value