C# HP QC ALM REST API:将附件上传到缺陷

C# HP QC ALM REST API: Upload attachments to Defect

我正在从事 ASP.Net 项目。我必须上传 HP QC ALM 12.5x 缺陷的附件。

身份验证部分已经完成并成功运行。

上传缺陷到附件,收到WebException was unhandled: The remote server returned an error: (415) Unsupported Media Type.

我写了下面的代码

string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundaryBytes = System.Text.Encoding.ASCII.GetBytes("--" + boundary + "\r\n");

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(HPALMUrl + "/rest/domains/" + HPALMDomain + "/projects/" + HPALMProject + "/attachments");
request.Method = "POST";
request.Accept = "application/json";
request.Headers.Add("Accept-Language", "en-US");
request.Headers.Add("Accept-Encoding", "gzip, deflate, sdch");
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)"; 
request.ContentType = "Content-Type: multipart/form-data; boundary=" + boundary;
request.AllowWriteStreamBuffering = false;
request.ContentLength = imageBytes.Length;
authRequest.KeepAlive = true;
request.CookieContainer = createSessionRequest.CookieContainer; //successfully authenticated cookie

string contentDisposition = "form-data; entity.type=\"{0}\"; entity.id=\"{1}\"; filename=\"{2}\"; Content-Type=\"{3}\"";
request.Headers.Add("Content-Disposition", string.Format(contentDisposition, "defect", "4", "Defect Attachment.png", "application/octet-stream")); //I am not clear with passing content disposition. Please let me know if this should be modified.

我正在尝试上传如下所示的图片文件

string path = @"C:\TestAttachment.png";
byte[] imageBytes = System.IO.File.ReadAllBytes(path);
using (Stream requestStream = request.GetRequestStream())
{
    requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
    using (MemoryStream memoryStream = new MemoryStream(imageBytes))
    {
        byte[] buffer = new byte[imageBytes.Length];
        int bytesRead = 0;
        while ((bytesRead = memoryStream.Read(buffer, 0, buffer.Length)) != 0)
        {
            requestStream.Write(buffer, 0, bytesRead);
        }
    }
}

var response = (HttpWebResponse)request.GetResponse(); //Receiving Error here

string responseText = string.Empty;
if (response.StatusCode == HttpStatusCode.OK)
    responseText = "Update completed";
else
    responseText = "Error in update";

请让我知道我做错了什么。

我也不确定能否通过rest api中提到的内容处理。

提前致谢!

def upload_result_file(self, defect_id, report_file):
    '''
        Function    :   upload_result_file
        Description :   Upload test result to ALM
    '''
    payload = open(report_file, 'rb')
    headers = {}
    headers['Content-Type'] = "application/octet-stream"
    headers['slug'] = "test-results" + report_file[report_file.rfind(".")+1: ]
    response = self.alm_session.post(ALM_URL + ALM_MIDPOINT + "/defects/" +
                                     str(defect_id) + "/attachments/",
                                     headers=headers, data=payload)
    if not (response.status_code == 200 or response.status_code == 201):
        print "Attachment step failed!", response.text, response.url, response.status_code
    return