【问题标题】:How to submit bugs to BugTracker.NET from C# application?如何从 C# 应用程序向 BugTracker.NET 提交错误?
【发布时间】:2009-10-05 18:17:43
【问题描述】:

阅读 BugTracker.NET 的文档页面
BugTracker.NET API Documentation 我意识到我需要使用 GET 或 POST,我不得不承认,我不太擅长。我想知道:

  • 是否有一个库可以用来轻松地从 C# 应用程序(或 VB.NET)向 BugTracker.NET 提交错误?
    或者,
  • 如果没有库。如何使用 GET 或 POST 向 BugTracker.NET 提交错误?

【问题讨论】:

    标签: c# winforms bug-tracking winforms-to-web bugtracker.net


    【解决方案1】:

    查看这个简单的示例from the documentation,了解如何使用 .Net 发出 POST 请求。只需确保根据 BugTracker.NET API 要求设置要发布的变量即可。

    【讨论】:

      【解决方案2】:

      下面是来自 BugTracker.NET 服务的代码,它从 pop3 服务器读取电子邮件,然后将它们作为错误提交到 insert_bug.aspx 页面。不过没必要这么复杂。

      仅调用此 URL 也可以:

      http:\\YOUR-HOST\insert_bug.aspx?username=YOU&password=YOUR-PASSWORD&short_desc=This+is+a+bug

      更复杂的代码:

      字符串 post_data = "username=" + HttpUtility.UrlEncode(ServiceUsername) + "&password=" + HttpUtility.UrlEncode(ServicePassword) + "&projectid=" + Convert.ToString(projectid) + "&from=" + HttpUtility.UrlEncode(来自) + "&short_desc=" + HttpUtility.UrlEncode(主题) + "&message=" + HttpUtility.UrlEncode(message); byte[] bytes = Encoding.UTF8.GetBytes(post_data); // 向网络服务器发送请求 HttpWebResponse res = null; 尝试 { HttpWebRequest req = (HttpWebRequest) System.Net.WebRequest.Create(Url); req.Credentials = CredentialCache.DefaultCredentials; req.PreAuthenticate = true; //req.Timeout = 200; // 也许? //req.KeepAlive = false; // 也许? req.Method = "POST"; req.ContentType = "应用程序/x-www-form-urlencoded"; req.ContentLength=bytes.Length; 流 request_stream = req.GetRequestStream(); request_stream.Write(bytes,0,bytes.Length); request_stream.Close(); res = (HttpWebResponse) req.GetResponse(); } 捕获(异常 e) { write_line("HttpWebRequest 错误 url=" + Url); write_line(e); } // 检查响应 如果 (res != null) { int http_status = (int) res.StatusCode; write_line (Convert.ToString(http_status)); 字符串 http_response_header = res.Headers["BTNET"]; 水库关闭(); 如果(http_response_header != null) { write_line (http_response_header); // 如果我们只删除来自 pop3 服务器的消息 // 知道我们存储在 Web 服务器上 ok if (MessageInputFile == "" && http_status == 200 && DeleteMessagesOnServer == "1" && http_response_header.IndexOf("OK") == 0) { write_line("发送 POP3 命令 DELE"); write_line (client.DELE (message_number)); } } 别的 { write_line("未找到 BTNET HTTP 标头。跳过从服务器删除电子邮件。"); write_line("递增总错误计数"); total_error_count++; } } 别的 { write_line("Web 服务器没有响应。跳过从服务器删除电子邮件。"); write_line("递增总错误计数"); total_error_count++; }

      【讨论】:

      • 这里的问题是您的“消息”在很多情况下可能会超过该 GET url 的最大长度。 POST 在这里更安全。
      • 对。较长的示例使用 POST。
      【解决方案3】:

      谢谢大家的回答。使用您的答案和网络上的其他资源,我整理了一种向 BugTracker.NET 提交新错误的方法
      该方法返回一个指示成功或失败的布尔值,并向用户显示一条带有状态的消息。
      可以更改此行为以满足您的需求。 该方法使用 POST 方法提交错误,这有助于在评论中提交任何长文本(我尝试在 cmets 中提交日志文件的内容并且它有效)。

      代码如下:

      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;
              }
          }
      

      【讨论】:

        【解决方案4】:

        我正在使用该方法,但不喜欢将密码与提交的错误一起发送。由于各种原因,我们使用的是内部 BugTracker 密码系统而不是 LDAP 身份验证,因此我们不知道他们的 BugTracker 密码。在我的例子中,我们所有的用户都被授权提交错误,他们的登录名是他们的 LAN ID。因此,我从他们经过身份验证的应用程序实例中收集他们报告的问题,捕获他们报告问题的项目 ID、程序和类,并调用 BugTracker DB 中的存储过程以直接插入项目。

        当然,不利的是,它直接进入数据库,可能会导致未来升级出现问题,但它现在对我们来说运行良好。

        (SQL2005/2008)

        CREATE PROCEDURE [dbo].[Add_Bug]
        @strUsername as varchar(20) = '', 
        @intProjID as integer = 0,
        @strSubject as varchar(200),
        @strComment as text
        AS
            BEGIN
        SET NOCOUNT ON;
        
        declare @us_id as integer
        declare @us_org as integer
        declare @st_id as integer
        declare @priority as integer
        declare @category as integer
        declare @errorreturn as integer
        declare @assigneduser as integer
        
        declare @newbugid as integer
        
        if (@intProjID = 0 or RTRIM(@strUsername) = '') 
            RETURN -1
        
        set @priority = 3 -- default to LOW
        set @category = 1 -- default to bug
        
        -- look up us_id, us_org from users where us_username = 'lanid'
        
        set @us_id = 0
        
        BEGIN TRY
        
            BEGIN TRANSACTION
        
            select @us_id = us_id, @us_org = us_org from BugTracker.dbo.users 
            where us_username = @strUsername
        
            if (@@ROWCOUNT = 0 or @us_id = 0 )
            BEGIN
                -- set to default values to allow entry anyway
                -- if not found default to the autobug reporter
                            -- this is a separate account created just for these reports
                set @us_id = 36     
                set @us_org = 6     
            END
        
            select @assigneduser = pj_default_user from projects 
                            where pj_id = @intProjID and 
                pj_auto_assign_default_user = 1
        
            if (@@ROWCOUNT <> 1)
                set @assigneduser = NULL
        
            -- get default status as st_id from statuses where st_default = 1
        
            select @st_id = st_id from BugTracker.dbo.statuses where st_default = 1 
        
            -- now insert the bug and post comments
        
            insert into bugs (bg_short_desc, bg_reported_user, bg_reported_date,
                    bg_status, bg_priority, bg_org, bg_category, bg_project,                     
                    bg_assigned_to_user, bg_last_updated_user, bg_last_updated_date) 
            values ( @strSubject, @us_id, getdate(), @st_id, @priority, @us_org,
                    @category, @intProjID, @assigneduser, @us_id, getdate())
        
            if @@ERROR <> 0
                BEGIN
                ROLLBACK TRANSACTION
                END
            ELSE
                BEGIN
        
                select @newbugid = @@IDENTITY
        
                insert into bug_posts (bp_bug, bp_type, bp_user, bp_date,
                            bp_comment, bp_hidden_from_external_users)
                values (@newbugid, 'comment', @us_id, getdate(), @strComment, 0)
        
                if @@ERROR <> 0
                    ROLLBACK TRANSACTION
                END
        END TRY
        
        BEGIN CATCH
            ROLLBACK TRANSACTION
            RETURN -2
        END CATCH   
        
        IF (@@TRANCOUNT > 0)
            COMMIT TRANSACTION
        
        RETURN @newbugid
        
        END
        

        【讨论】:

          猜你喜欢
          • 2011-10-29
          • 2014-08-30
          • 1970-01-01
          • 1970-01-01
          • 2015-06-16
          • 2015-04-16
          • 2015-11-08
          • 2017-11-27
          • 2016-01-04
          相关资源
          最近更新 更多