1
我將通過JIRA REST API在C#應用程序中更新JIRA中的問題的工作日誌。以下代碼顯示了迄今爲止所做的工作。C#JIRA工作日誌更新錯誤「遠程服務器返回錯誤:(401)未經授權。」
HttpWebResponse返回此錯誤「遠程服務器返回錯誤:(401)未經授權。」。
我試着用相同的證書和在PHP cURL函數中使用相同的數據,它工作正常,併成功地更新工作日誌。
這是我的Jason轉換後的序列化對象:{「update」:{「worklog」:[{「add」:{「comment」:「IJ的樣本測試評論」,「timeSpent」:「210」}} }]}}
protected string RunQuery(JiraResource resource, string argument = null, string data = null, string method = "PUT")
{
// Where;
// resource = issue
// argument = "JIRA-16"
// Data = {"update":{"worklog":[{"add":{"comment":"Sample test comment by IJ","timeSpent":"210"}}]}}
// Method = "PUT"
string url = string.Format("{0}{1}/", m_BaseUrl, resource.ToString());
if (argument != null)
{
url = string.Format("{0}{1}", url, argument);
}
// URL = https://companyname.atlassian.net/rest/api/2/issue/JIRA-16
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
request.ContentType = "application/json";
request.Method = method;
request.ContentLength = data.Length;
using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
{
writer.Write(data);
}
string base64Credentials = GetEncodedCredentials(); // check below
request.Headers.Add("Authorization", "Basic " + base64Credentials);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;// here returns the error
//The remote server returned an error: (401) Unauthorized.
string result = string.Empty;
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
result = reader.ReadToEnd();
}
return result;
}
private string GetEncodedCredentials()
{
string mergedCredentials = string.Format("{0}:{1}", m_Username, m_Password);
byte[] byteCredentials = UTF8Encoding.UTF8.GetBytes(mergedCredentials);
return Convert.ToBase64String(byteCredentials);
}
我在哪裏做錯了?請幫幫我。
總是發生!謝謝阿列克謝..它工作正常!!!!!!! – iJay