views:

292

answers:

3

Reading the documentation page of BugTracker.NET
BugTracker.NET API Documentation I realized that I need to use GET or POST which, I have to admit, I'm not very good at. I was wondering:

  • Is there a library that could be used to easily submit bugs to BugTracker.NET from a C# application (or VB.NET) ?
    Or,
  • If there's no library. How can use GET or POST to submit bugs to BugTracker.NET ?
+2  A: 

Check this simple example http://j.mp/VaAH0 of how to make a POST request using .Net. Just make sure to set up the variables being POSTed in according to BugTracker.NET API requirements.

Mad
+1  A: 
Corey Trager
Problem here is your "message" will likely exceed the max length of that GET url in a lot of cases. POST is safer here.
Anderson Imes
Right. The longer example uses POST.
Corey Trager
A: 

Thank you all for your answers. Using your answers and other resources on the web, I've put together a method for submitting a new bug to BugTracker.NET
The method returns a boolean value indicating success or failure and it displays a message to the user with the status.
This behavior could be changed to match your needs. The method uses POST method to submit bugs which helps to submit any long text in the comment (I've tried to submit the content of a log file in the comments and it worked).

Here's the code:

public bool SubmitBugToBugTracker(string serverName,
                                        bool useProxy,
                                        string proxyHost,
                                        int proxyPort,
                                        string userName,
                                        string password,
                                        string description,
                                        string comment,
                                        int projectId)
    {
        if (!serverName.EndsWith(@"/"))
        {
            serverName += @"/";
        }
        string requestUrl = serverName + "insert_bug.aspx";
        string requestMethod = "POST";
        string requestContentType = "application/x-www-form-urlencoded";
        string requestParameters = "username=" + userName
                                  + "&password=" + password
                                  + "&short_desc=" + description
                                  + "&comment=" + comment
                                  + "&projectid=" + projectId;
        // POST parameters (postvars)
        byte[] buffer = Encoding.ASCII.GetBytes(requestParameters);
        // Initialisation
        HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(requestUrl);
        // Add proxy info if used.
        if (useProxy)
        {
            WebReq.Proxy = new WebProxy(proxyHost, proxyPort);
        }

        // Method is POST
        WebReq.Method = requestMethod;
        // ContentType, for the postvars.
        WebReq.ContentType = requestContentType;
        // Length of the buffer (postvars) is used as contentlength.
        WebReq.ContentLength = buffer.Length;
        // Open a stream for writing the postvars
        Stream PostData = WebReq.GetRequestStream();
        //Now we write, and afterwards, we close. Closing is always important!
        PostData.Write(buffer, 0, buffer.Length);
        PostData.Close();
        // Get the response handle, we have no true response yet!
        HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
        // Read the response (the string)
        Stream Answer = WebResp.GetResponseStream();
        StreamReader _Answer = new StreamReader(Answer);
        string responseStream = _Answer.ReadToEnd();

        // Find out if bug submission was successfull.
        if (responseStream.StartsWith("OK:"))
        {
            MessageBox.Show("Bug submitted successfully.");
            return true;
        }
        else if (responseStream.StartsWith("ERROR:"))
        {
            MessageBox.Show("Error occured. Bug hasn't been submitted.\nError Message: " + responseStream);
            return false;
        }
        else
        {
            MessageBox.Show("Error occured. Bug hasn't been submitted.\nError Message: " + responseStream);
            return false;
        }
    }
Amr