Https上传MultipartForm返回401未授权

本文关键字:授权 返回 上传 MultipartForm Https | 更新日期: 2023-09-27 17:51:11

我正在尝试上传coverity扫描结果到coverity。

这是我的代码:

public static void Main( String[] args )
{
    var client = new HttpClient
    {
        Timeout = TimeSpan.FromMinutes( 20 )
    };
    var form = new MultipartFormDataContent
    {
        { new StringContent( "my tooken" ), "token" },
        { new StringContent( "my email" ), "email" },
        { new StringContent( "1.1.1.1" ), "version" },
        { new StringContent( "Test..." ), "description" }
    };
    var fs = new FileStream( @"cov-int.zip", FileMode.Open, FileAccess.Read );
    form.Add(new StreamContent(fs), "file", "cov-int.zip");
    var task = client.PostAsync("https://scan.coverity.com/builds?project=Name/Porject", form);
    try
    {
        task.Wait();
    }
    catch ( AggregateException ex )
    {
        throw ex.InnerException;
    }
    var result = task.Result;
    fs.Close();
}

邮件总是以失败状态(401未授权)结束:

{StatusCode: 401, ReasonPhrase: 'Unauthorized', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
  x-xss-protection: 1; mode=block
  x-request-id: 70dfc119-7d78-47fe-86a7-8505d73225e4
  x-runtime: 1.675468
  x-frame-options: SAMEORIGIN
  x-content-type-options: nosniff
  Connection: close
  Status: 401 Unauthorized
  Transfer-Encoding: chunked
  Cache-Control: no-cache
  Date: Fri, 29 May 2015 13:01:06 GMT
  Server: Apache
  X-Powered-By: Phusion Passenger 5.0.8
  Content-Type: text/html; charset=utf-8
}}

我试图上传相同的数据,从同一台机器到同一台服务器使用curl:

curl --form token="token" --form email="email" --form file="cov-int.zip" --form version="1.1.1.1" --form description="a message" --insecure https://scan.coverity.com/builds?project=Name/Project

使用curl上传数据有效。

我在c#代码中做错了什么?

Https上传MultipartForm返回401未授权

Coverity端可能发生了一些变化,因为这两周前还在工作。基于这另一个问题,这里如何上传文件到Asp。.Net MVC 4.0操作运行在IIS Express与。Net 4.0中包含的HttpClient类,我已经找到了我认为是解决方案。

你需要在你的表单数据名称中添加额外的引号。

var form = new MultipartFormDataContent
{
    { new StringContent( "my tooken" ), "'"token'"" },
    { new StringContent( "my email" ), "'"email'"" },
    { new StringContent( "1.1.1.1" ), "'"version'"" },
    { new StringContent( "Test..." ), "'"description'"" }
};
form.Add(new StreamContent(fs), "'"file'"", "cov-int.zip");

不能100%确定这是否有效,因为我用完了今天所有的尝试,必须等到明天才能看到成功的回应。但是我现在收到的是"The build submission quota for this project has been reached."消息,而不是Access Denied消息。