2013-08-16 49 views
0

林捆紮使用REST柱從MS SSIS進程發送數據:Atlassian的JIRA C#REST POST

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 "Package.dtsx" starting. Error: 0x1 at Script Task: System.Reflection.TargetInvocationException: Het doel van een aanroep heeft een uitzondering veroorzaakt. ---> System.Net.WebException: De externe server heeft een fout geretourneerd: (400) Ongeldige opdracht. bij System.Net.HttpWebRequest.GetResponse() bij ST_8fbfe559ee824ef19f7c9bc2c425effc.csproj.ScriptMain.Main() --- Einde van intern uitzonderingsstackpad --- bij System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner) bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks) bij System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
bij System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams) bij Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTATaskScriptingEngine.ExecuteScript() Task failed: Script Task Warning: 0x80019002 at Package: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED. The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors. SSIS package "Package.dtsx" finished: Failure.

的錯誤消息的英語位是

The purpose of a call has caused an exception. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request

+0

但我不明白爲什麼會出現這種情況,jira文檔指出這是正確的代碼:https://developer.atlassian.com/static/rest/jira/5.2.5.html#id326535 – user1114720

回答

0

我與我的應用程序測試您的JSON,它似乎是正確的(假設你的專案編號和issueTypeName是正確的FO你的Jira)。 (EDIT2:確保您發送所有您要創建的issueType的必填字段)。

但是,爲什麼你不在頭中添加授權?

此外,我將我的JSON編碼爲UTF8,然後發佈。不確定您的StreamWriter是否也這樣做。 (EDIT1:好吧,我道歉,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); 
} 

我可以添加我所有的方法,如果這段代碼不幫助你。

相關問題