كيفية تقديم الأخطاء إلى bugtracker.net من تطبيق C #؟
-
19-09-2019 - |
سؤال
قراءة صفحة الوثائق من bugtracker.net
وثائق API BugTracker.netأدركت أنني بحاجة إلى استخدام الحصول على أو نشر أي، يجب أن أعترف، أنا لست جيدا جدا في. كنت أتساءل:
- هل هناك مكتبة يمكن استخدامها بسهولة إرسال الأخطاء إلى BugTracker.net من تطبيق C # (أو vb.net)؟
أو، - إذا لم يكن هناك مكتبة. كيف يمكن استخدام الحصول على أو النشر لإرسال الأخطاء إلى bugtracker.net؟
المحلول 3
شكرا لكم جميعا على إجاباتك. باستخدام إجاباتك وغيرها من الموارد على الويب، قمت بتجميع طريقة لتقديم خطأ جديد إلى BugTracker.net
ترجع الطريقة قيمة منطقية تشير إلى النجاح أو الفشل وتعرض رسالة للمستخدم مع الحالة.
يمكن تغيير هذا السلوك لمطابقة احتياجاتك. تستخدم الطريقة طريقة النشر لإرسال الأخطاء التي تساعد على تقديم أي نص طويل في التعليق (حاولت تقديم محتوى ملف السجل في التعليقات وعمل).
إليك الرمز:
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;
}
}
نصائح أخرى
تحقق من هذا المثال البسيط من الوثائق من كيفية تقديم طلب بعد باستخدام .NET. فقط تأكد من إعداد المتغيرات التي يتم نشرها في متطلبات BugTracker.net API.
فيما يلي رمز خدمة BugTracker.net الذي يقرأ رسائل البريد الإلكتروني من خادم POP3 ثم يقدمها كخطاء إلى صفحة Insert_bug.aspx. ولكن لا يجب أن يكون هذا معقدا.
فقط استدعاء عنوان URL سيعمل أيضا:
http: مضيف الخاص بك insert_bug.aspx؟ اسم المستخدم = أنت وكلمة المرور = كلمة المرور الخاصة بك و short_desc = هذا + هو + علة +
رمز أكثر تعقيدا:
string post_data = "اسم المستخدم =" + httputility.urlencode (serviceus اسمها) + "وكلمة المرور =" + httputility.urlencode (servicepassword) + "& projectid =" + convert.tostring (projectid) + "& from =" + httputility.urlencode (من) + "& short_desc =" + httputility.urlencode (الموضوع) + "& message =" + httputility.urlencode (رسالة)؛ بايت [] بايت = encoding.utf8.getytes (post_data)؛ // إرسال طلب إلى خادم الويب httpwebresponse res = null؛ جرب {httpwebrequest req = (httpwebrequest) system.net.webrequest.create (URL)؛ Req.Credentials = redentalexcache.defaultcredentials؛ req.preauthenticate = صحيح؛ //req.timeout = 200؛ // يمكن؟ //req.keepalive = خطأ؛ // يمكن؟ req.method = "المشاركة"؛ req.contenttype = "تطبيق / x-www-reslencoded"؛ req.contentlength = bytes.length؛ دفق طلب_stream = req.getRequeststream ()؛ request_stream.write (بايت، 0، bytes.length)؛ request_stream.close ()؛ res = (httpwebresponse) req.getResponse ()؛ } الصيد (استثناء E) {trite_line ("url url url url =" + url)؛ Write_line (E)؛ } // فحص الاستجابة إذا (res! = null) {int http_status = (int) res.statuscode؛ Write_line (convert.toststring (http_status))؛ سلسلة http_response_header = res.headers ["btnet"]؛ res.close ()؛ إذا (http_response_header! = null) {triting_line (http_response_header)؛ // فقط حذف الرسالة من خادم POP3 إذا عرفنا علينا // تخزينها في خادم الويب موافق إذا (messageInputFile == "&& http_status == 200 && deletemessagesonserver ==" 1 "&& http_response_header.indexof (" موافق ") = = 0) {trite_line ("إرسال command dele dele")؛ Write_line (client.dele (message_number))؛ }}} آخر {"BTNET HTTP Header غير موجود. تخطي حذف البريد الإلكتروني من الخادم")؛ Write_line ("زيادة عدد الأخطاء الإجمالية")؛ total_error_count ++؛ }}} {trite_line ("لا توجد استجابة من خادم الويب. تخطي حذف البريد الإلكتروني من الخادم.")؛ Write_line ("زيادة عدد الأخطاء الإجمالية")؛ total_error_count ++؛ }
كنت أستخدم هذه الطريقة ولكن لم يعجبك إرسال كلمة المرور إلى جانب الخطأ المقدم. لأسباب مختلفة، نحن نستخدم نظام كلمة مرور BugTracker الداخلية وليس مصادقة LDAP، بحيث لا يعرفنا كلمات مرور BugTracker الخاصة بهم. في حالتي، يسمح لجميع مستخدمينا بتقديم الأخطاء، وتسجيل الدخول الخاص بهم هو معرف LAN الخاص بهم. لذلك، من مثيلهم المصادق عليه للتطبيق، أقوم بجمع مسألة الإبلاغ عنها، والتقاط معرف المشروع والبرنامج والفئة حيث يقومون بالإبلاغ عن المشكلة، واتصل بإجراءات مخزنة في 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