Json.Ping C# (CSharp) Method

Ping() public method

public Ping ( string osVersion, string cjVersion, string machineID ) : bool
osVersion string
cjVersion string
machineID string
return bool
    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;
    }

Usage Example

コード例 #1
0
ファイル: chronojump.cs プロジェクト: GNOME/chronojump
    private void pingDo(bool showInWindow)
    {
        jsPing = new Json();
        bool success = jsPing.Ping(UtilAll.GetOS(), UtilAll.ReadVersionFromBuildInfo(), preferences.machineID);

        if(success) {
            LogB.Information(jsPing.ResultMessage);
            if(showInWindow)
                new DialogMessage(
                        "Chronojump",
                        Constants.MessageTypes.INFO,
                        jsPing.ResultMessage);
        }
        else {
            LogB.Error(jsPing.ResultMessage);
            if(showInWindow)
                new DialogMessage(
                        "Chronojump",
                        Constants.MessageTypes.WARNING,
                        jsPing.ResultMessage);
        }

        /*
        new DialogMessage(
                "Chronojump",
                Constants.MessageTypes.INFO,
                "Temporarily Disabled");
        */
    }