簡體   English   中英

ATLASSIAN JIRA C#REST POST

[英]ATLASSIAN JIRA C# REST POST

打算使用REST帖子從MS SSIS進程發送數據:

    json = json + "{ ";
        json = json + "\"fields\": {";
        json = json + "\"project\": {  \"id\": \"XXX\" },";
        json = json + "\"summary\": \"REST ye merry gentlemen.\",";
        json = json + "\"description\": \"Hellow\",";
        json = json + "\"issuetype\": { \"name\": \"Bug\" }";
        json = json + "} ";
        json = json + "} ";


        var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://xxx.atlassian.net/rest/api/2/issue/");
        httpWebRequest.ContentType = "application/json";
        httpWebRequest.Method = "POST";


        using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
        {

            MessageBox.Show(json);

            streamWriter.Write(json);
        }

        string mergedCredentials = string.Format("{0}:{1}", "xxx", "xxx");
        byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
        string credentials = Convert.ToBase64String(byteCredentials);

        //httpWebRequest.Headers.Add("Authorization", "Basic " + credentials);

        var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();

        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            var responseText = streamReader.ReadToEnd();
            //Now you have your response.
            //or false depending on information in the response

        } 

服務器響應:

SSIS程序包“ Package.dtsx”開始。 錯誤:腳本任務:System.Reflection.TargetInvocationException處為0x1:Het doel van een aanroep heeft een uitzondering veroorzaakt。 ---> System.Net.WebException:服務器外部故障發生的原因:(400)Ongeldige opdracht。 bij System.Net.HttpWebRequest.GetResponse()bij ST_8fbfe559ee824ef19f7c9bc2c425effc.csproj.ScriptMain.Main()--- Einde van intern uitzonderingsstackpad --- bij System.RuntimeMethodHandle._InvokeMethodss(對象目標,Object []方法和屬性,對象方法,屬性[],屬性,RuntimeTypeHandle typeOwner)bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder活頁夾,Object []參數,CultureInfo文化,布爾skipVisibilityChecks)bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj,BindingFlags invokeAttr,Binder活頁夾,Object []參數,CultureInfo文化)
bij System.RuntimeType.InvokeMember(字符串名稱,BindingFlags bindingFlags,活頁夾活頁夾,對象目標,Object []提供的Args,ParameterModifier []修飾符,CultureInfo文化,String []命名為Params)bij Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine。 ExecuteScript()任務失敗:腳本任務警告:程序包處的0x80019002:SSIS警告代碼DTS_W_MAXIMUMERRORCOUNTREACHED。 執行方法成功,但是引發的錯誤數(1)達到允許的最大值(1); 導致失敗。 錯誤數量達到MaximumErrorCount中指定的數量時,會發生這種情況。 更改MaximumErrorCount或修復錯誤。 SSIS程序包“ Package.dtsx”已完成:失敗。

錯誤消息的英文位是

通話的目的引起了異常。 ---> System.Net.WebException:遠程服務器返回錯誤:(400)錯誤的請求

我用我的應用程序測試了您的JSON,它似乎是正確的(假設您的projectId和issueTypeName對於您的Jira是正確的)。 EDIT2 :請確保發送您要創建的issueType的所有必填字段)。

但是,為什么不在標題中添加授權?

另外,我在發布JSON之前將其JSON編碼為UTF8。 不知道您的StreamWriter是否也這樣做。 編輯1 :好的,我很抱歉,將StreamWriter編碼為UTF-8,但是我的代碼仍然與request.ContentLength和request.Accept有所不同,請再次確認是否會有所不同)

byte[] data = Encoding.UTF8.GetBytes(postData);
request.Accept = "application/json";
request.ContentLength = data.Length;
using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(data, 0, data.Length);
}

如果此代碼段無法幫助您,則可以添加所有方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM