https上传MultipartForm returns 401未授权
Https upload MultipartForm returns 401 unauthorized
我正在尝试将 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();
}
post 总是以失败状态(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# 代码哪里做错了?
Coverity 端可能发生了一些变化,因为它在两周前就可以工作了。基于这里的另一个问题 我找到了我认为的解决方案。
您需要为表单数据名称添加额外的引号。
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."
消息,而不是拒绝访问消息。
我正在尝试将 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();
}
post 总是以失败状态(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# 代码哪里做错了?
Coverity 端可能发生了一些变化,因为它在两周前就可以工作了。基于这里的另一个问题 我找到了我认为的解决方案。
您需要为表单数据名称添加额外的引号。
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."
消息,而不是拒绝访问消息。