2015-05-29 113 views
0

我試圖上傳coverity掃描結果以覆蓋。Https上傳MultipartForm返回401未授權

這是我的代碼:

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 --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 

使用捲曲工作上傳數據。

我在C#代碼中做錯了什麼?

+0

在我看來,你的C#代碼沒有以正確的格式發佈HTTP請求。看看如何確保它是一個多部分Post HTTP請求。 – AndreCruz

+0

我無法找到任何錯誤的格式化數據。請求看起來不錯。 – musium

回答

1

Coverity的結局可能發生了變化,因爲這是兩週前的工作。基於這裏的其他問題How to upload files to Asp.Net MVC 4.0 action running in IIS Express with HttpClient class included in .Net 4.0我發現我認爲是解決方案。

您需要爲表單數據名稱添加額外的引號。

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."消息而不是「拒絕訪問」消息。