【问题标题】:C# HP QC ALM REST API: Upload attachments to DefectC# HP QC ALM REST API:将附件上传到缺陷
【发布时间】:2017-11-15 09:57:47
【问题描述】:

我正在开发 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 中提到的内容处置。

提前致谢!

【问题讨论】:

  • Content-Disposition 是一个 response 标头。 服务器 将在其响应中设置它。根本不要在请求中设置它
  • 至于错误本身,附件是文件,而您的Content-typemultipart/form-data。您没有发布表格。您正在创建一个新文件。也许您应该检查一下 HTTP 的一般工作原理和 REST 的具体工作原理?
  • @PanagiotisKanavos 但是,在 API reference 中,标题部分提到了 Content-Disposition
  • 在哪里?如果你搜索它,你不会找到那个词
  • 我找到了。它根本不是 REST API,只是表单发布。

标签: c# asp.net rest hp-quality-center


【解决方案1】:
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

【讨论】:

  • 我让它工作了。你总是帮助我。感谢您的代码!
  • @KarthickRaju 可以分享我的 C# 代码。在上面的代码中,作为 self 传递什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多