将具有令牌身份验证的cURL表单文件发布转换为C#

时间:2018-09-27 15:01:05

标签: c# curl httpwebrequest httpclient webclient

我正在尝试使用C#将cURL下面的内容转换为HttpClient或类似内容。

curl --form "file=@C:\files\test.csv" https://company.com/api/v0/imports/api_name?token=123456asdfgh2345asdfg12345sdfgh2345sdfg123asd

我一直在尝试下面的操作,但是它给我扔了401。

  

状态码:401,原因短语:“未经授权”,版本:1.1,内容:System.Net.Http.StreamContent,标头:

代码如下:

private async Task<string> PostAttachment(byte[] data, Uri url, string contentType, string token)
{
    HttpContent content = new ByteArrayContent(data);
    content.Headers.ContentType = new MediaTypeHeaderValue(contentType);
    using (var form = new MultipartFormDataContent())
    {   
        form.Add(content);
        using (var client = new HttpClient())
        {
            client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
            var response = await client.PostAsync(url, form);
            return await response.Content.ReadAsStringAsync(); }}}

这里叫

var uri = new Uri(@"https://company.com/api/v0/imports/api_name");
PostAttachment(Encoding.UTF8.GetBytes(csv), uri, "text/csv", token);`

我在这里尝试了许多其他类似的帖子,但是没有通过令牌进行身份验证。

0 个答案:

没有答案
相关问题