public bool Ping(string osVersion, string cjVersion, string machineID)
{
requestPingAborting = false;
// Create a request using a URL that can receive a post.
requestPing = WebRequest.Create (serverUrl + "/ping");
// Set the Method property of the request to POST.
requestPing.Method = "POST";
// Set the ContentType property of the WebRequest.
requestPing.ContentType = "application/json";
// Creates the json object
JsonObject json = new JsonObject();
json.Add("os_version", osVersion);
json.Add("cj_version", cjVersion);
json.Add("machine_id", machineID);
// Converts it to a String
String js = json.ToString();
// Writes the json object into the request dataStream
Stream dataStream;
try {
dataStream = requestPing.GetRequestStream ();
} catch {
this.ResultMessage =
string.Format(Catalog.GetString("You are not connected to the Internet\nor {0} server is down."),
serverUrl);
return false;
}
if(requestPingAborting) {
LogB.Information("Aborted from PingAbort");
return false;
}
dataStream.Write (Encoding.UTF8.GetBytes(js), 0, js.Length);
dataStream.Close ();
// Get the response.
WebResponse response;
try {
response = requestPing.GetResponse ();
} catch {
this.ResultMessage =
string.Format(Catalog.GetString("You are not connected to the Internet\nor {0} server is down."),
serverUrl);
return false;
}
if(requestPingAborting) {
LogB.Information("Aborted from PingAbort");
return false;
}
// Display the status (will be 201, CREATED)
Console.WriteLine (((HttpWebResponse)response).StatusDescription);
// Clean up the streams.
dataStream.Close ();
response.Close ();
this.ResultMessage = "Ping sent.";
return true;
}