我正在尝试将一个工作的postman POST调用实现到一个c#控制台应用程序,它正在给我:禁止403。 它正在调用API并且正在上传CSV文件。 这是邮递员的工作电话:
HttpClient c = new HttpClient();
var fileContent = new ByteArrayContent(System.IO.File.ReadAllBytes(@"C:\test7.csv"));
fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "test7.csv"
};
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("text/csv");
var parameters = new Dictionary<string, string>
{
{ "secret", "mypassword" }
};
HttpContent DictionaryItems = new FormUrlEncodedContent(parameters);
MultipartContent content = new MultipartContent();
//content.Add(formData);
content.Add(DictionaryItems);
content.Add(fileContent);
c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/x-www-form-urlencoded"));
var resultado = c.PostAsync("https://www.apiurl,com", content).Result;
我认为我没有把秘密(密码)放在正确的位置。 有什么帮助吗?
答案 0 :(得分:-1)
我得到了答案。这里:
FileStream stream = File.OpenRead(@"C:\test7.csv");
byte[] fileBytes = new byte[stream.Length];
stream.Read(fileBytes, 0, fileBytes.Length);
stream.Close();
var byteArrayContent = new ByteArrayContent(fileBytes);
byteArrayContent.Headers.ContentType = MediaTypeHeaderValue.Parse("text/csv");
HttpClient httpClient = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(new StringContent("mypassword"), "secret");
form.Add(byteArrayContent, "file", "testT.csv");
HttpResponseMessage response = httpClient.PostAsync("https://www.apiurl.com", form).Result;
httpClient.Dispose();