简体   繁体   English

C# 二进制文件发布请求

[英]C# Binary File Post Request

I am dealing with a third-party API which insists on a binary File Upload request being formatted without a Content-Type header value of multipart/form-data , and with the following headers:我正在处理第三方 API ,它坚持二进制文件上传请求被格式化,没有Content-Type header 值multipart/form-data ,并具有以下标题:

Content-Type: application/octet-stream
Content-Disposition: filename*=UTF-8''file.zip

HttpRequestMessage and HttpContent.Headers.ContentDisposition.DispositionType won't allow me to achieve this either because I can't set the values as desired or they set them automatically. HttpRequestMessageHttpContent.Headers.ContentDisposition.DispositionType不允许我实现这一点,因为我无法根据需要设置值,或者它们会自动设置它们。

I accept that this API may not be following HTTP Standards but it's not mine and I have no influence over it.我接受这个 API 可能不遵循 HTTP 标准,但它不是我的,我对它没有影响。

My attempt which does not work我的尝试不起作用

    using (HttpClient client = new HttpClient())
    {
        client.DefaultRequestHeaders.ExpectContinue = false;
        FileStream fs = new FileStream(@"e:\dev\TestHalfB.docx", FileMode.Open);

        HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, <Uri>);
        HttpContent fc = new StreamContent(fs);
        var mpContent = new MultipartFormDataContent();
        mpContent.Add(fc);
        fc.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
        req.Content = fc;
        fc.Headers.ContentDisposition.DispositionType = "filename*=UTF-8''TestHalfB.docx";

        using (var response = await client.SendAsync(req))
        {
            response.EnsureSuccessStatusCode();
            var resp = await response.Content.ReadAsStringAsync();
        }

        fs.Close();
    }

Does anyone know of a lower level API I could use or have any suggestions?有谁知道我可以使用的较低级别的 API 或有什么建议?

So the crux is how can I set the Content-Disposition header to the value I desire.所以关键是如何将Content-Disposition header 设置为我想要的值。

Could you please try this.请你试试这个。

   HttpContent fileStreamContent = new StreamContent(paramFileStream);

    using (var formData = new MultipartFormDataContent())
    {
        formData.Add(fileStreamContent, "file1", "file1");
        var response = client.PostAsync(actionUrl, formData).Result;
    }

This should work for you (or at least get you started, since it's not tested):这应该对你有用(或者至少让你开始,因为它没有经过测试):

HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, <Uri>);
using (FileStream fs = new FileStream(@"e:\dev\TestHalfB.docx", FileMode.Open))
{
    byte[] fb = new byte[(int)fs.Length]; // assumes your file size will fit into an int
    await fs.ReadAsync(fb, 0, (int)fs.Length);
    req.Content = new ByteArrayContent(fb);
    req.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/octet-stream");
    req.Content.Headers.ContentDisposition.FileNameStar = "UTF-8''TestHalfB.docx";

    using (var response = await client.SendAsync(req))
    {
        response.EnsureSuccessStatusCode();
        var resp = await response.Content.ReadAsStringAsync();
    }
}

I had to switch to using WebRequest .我不得不改用WebRequest

    WebRequest request = WebRequest.Create("https://cloud.memsource.com/web/api2/v1/projects/{id}/jobs?token={token}");
    request.Method = "POST";
    byte[] byteArray = File.ReadAllBytes(@"E:\Dev\TestHalfB.docx");
    request.ContentType = "application/octet-stream";
    request.ContentLength = byteArray.Length;
    request.Headers.Add("Content-Disposition", "filename*=UTF-8''TestHalfB.docx");
    Stream dataStream = request.GetRequestStream();
    dataStream.Write(byteArray, 0, byteArray.Length);
    dataStream.Close();
    WebResponse response = request.GetResponse();
    ((HttpWebResponse)response).StatusDescription.Dump();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM