Json.PostCrashLog C# (CSharp) Method

PostCrashLog() public method

public PostCrashLog ( string email, string comments ) : bool
email string
comments string
return bool
    public bool PostCrashLog(string email, string comments)
    {
        string filePath = UtilAll.GetLogFileOld();

        if(! File.Exists(filePath)) {
            this.ResultMessage = Catalog.GetString("Could not send file.\nIt does not exist.");
            return false;
        }

        if(comments != null && comments != "")
            Util.InsertTextBeginningOfFile(
                    "----------\nUser comments:\n" + comments + "\n----------\n", filePath);

        // Create a request using a URL that can receive a post.
        WebRequest request = WebRequest.Create (serverUrl + "/backtrace/" + UtilAll.ReadVersionFromBuildInfo() + "-" + email);

        // Set the Method property of the request to POST.
        request.Method = "POST";

        // Create POST data and convert it to a byte array.
        byte[] byteArray = readFile(filePath);

        // Set the ContentType property of the WebRequest.
        request.ContentType = "application/x-www-form-urlencoded";

        // Set the ContentLength property of the WebRequest.
        request.ContentLength = byteArray.Length;

        // Get the request stream.
        Stream dataStream;
        try {
            dataStream = request.GetRequestStream ();
        } catch {
            LogB.Warning("Error sending datastream");
            this.ResultMessage = Catalog.GetString("Could not send file.") + "\n" +
                string.Format(Catalog.GetString("You are not connected to the Internet\nor {0} server is down."),
                        serverUrl);
            return false;
        }

        // Write the data to the request stream.
        dataStream.Write (byteArray, 0, byteArray.Length);

        // Close the Stream object.
        dataStream.Close ();

        // Get the response.
        WebResponse response;
        try {
            response = request.GetResponse ();
        } catch {
            LogB.Warning("Error getting response");
            this.ResultMessage = Catalog.GetString("Could not send file.") + "\n" +
                string.Format(Catalog.GetString("You are not connected to the Internet\nor {0} server is down."),
                        serverUrl);
            return false;
        }

        // Display the status.
        LogB.Information(((HttpWebResponse)response).StatusDescription);

        // Get the stream containing content returned by the server.
        dataStream = response.GetResponseStream ();

        // Open the stream using a StreamReader for easy access.
        StreamReader reader = new StreamReader (dataStream);

        // Read the content.
        string responseFromServer = reader.ReadToEnd ();

        // Display the content.
        LogB.Information(responseFromServer);

        // Clean up the streams.
        reader.Close ();
        dataStream.Close ();
        response.Close ();

        JsonValue result = JsonValue.Parse(responseFromServer);
        string crash_id = result["crash_id"];
        LogB.Information("crash_id: ", crash_id);

        this.ResultMessage = Catalog.GetString("Log sent. Thank you.");
        return true;
    }

Usage Example

コード例 #1
0
    private void on_button_send_log_clicked(object o, EventArgs args)
    {
        string email = entry_send_log.Text.ToString();

        //email can be validated with Util.IsValidEmail(string)
        //or other methods, but maybe there's no need of complexity now

        //1st save email on sqlite
        if (email != null && email != "" && email != "0" && email != emailStored)
        {
            SqlitePreferences.Update("email", email, false);
        }

        //2nd add language as comments
        string language = get_send_log_language();

        SqlitePreferences.Update("crashLogLanguage", language, false);
        string comments = "Answer in: " + language + "\n";

        //3rd if there are comments, add them at the beginning of the file
        comments += textview_comments.Buffer.Text;

        //4th send Json
        Json js      = new Json();
        bool success = js.PostCrashLog(email, comments);

        if (success)
        {
            button_send_log.Label     = Catalog.GetString("Thanks");
            button_send_log.Sensitive = false;

            image_send_log_yes.Show();
            image_send_log_no.Hide();
            LogB.Information(js.ResultMessage);
        }
        else
        {
            button_send_log.Label = Catalog.GetString("Try again");

            image_send_log_yes.Hide();
            image_send_log_no.Show();
            LogB.Error(js.ResultMessage);
        }

        label_send_log_message.Text = js.ResultMessage;
    }
All Usage Examples Of Json::PostCrashLog