C# HttpClient 4.5 multipart/form

2018-07-06  本文已影响340人  faith3729

原文链接:https://stackoverflow.com/questions/16416601/c-sharp-httpclient-4-5-multipart-form-data-upload
https://blog.csdn.net/xcymorningsun/article/details/52950107

private static void Upload()
{
using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Add("User-Agent", "CBS Brightcove API Service");

    using (var content = new MultipartFormDataContent())
    {
        var path = @"C:\B2BAssetRoot\files\596086\596086.1.mp4";

        string assetName = Path.GetFileName(path);

        var request = new HTTPBrightCoveRequest()
            {
                Method = "create_video",
                Parameters = new Params()
                    {
                        CreateMultipleRenditions = "true",
                        EncodeTo = EncodeTo.Mp4.ToString().ToUpper(),
                        Token = "x8sLalfXacgn-4CzhTBm7uaCxVAPjvKqTf1oXpwLVYYoCkejZUsYtg..",
                        Video = new Video()
                            {
                                Name = assetName,
                                ReferenceId = Guid.NewGuid().ToString(),
                                ShortDescription = assetName
                            }
                    }
            };

        //Content-Disposition: form-data; name="json"
        var stringContent = new StringContent(JsonConvert.SerializeObject(request));
        stringContent.Headers.Add("Content-Disposition", "form-data; name=\"json\"");
        content.Add(stringContent, "json");

        FileStream fs = File.OpenRead(path);

        var streamContent = new StreamContent(fs);
        streamContent.Headers.Add("Content-Type", "application/octet-stream");
        //Content-Disposition: form-data; name="file"; filename="C:\B2BAssetRoot\files\596090\596090.1.mp4";
        streamContent.Headers.Add("Content-Disposition", "form-data; name=\"file\"; filename=\"" + Path.GetFileName(path) + "\"");
        content.Add(streamContent, "file", Path.GetFileName(path));

        //content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");

        Task<HttpResponseMessage> message = client.PostAsync("http://api.brightcove.com/services/post", content);

        var input = message.Result.Content.ReadAsStringAsync();
        Console.WriteLine(input.Result);
        Console.Read();
    }
}
}

总结

【客户端】如何传参数到服务器?

        //Content-Disposition: form-data; name="json"
        var stringContent = new StringContent(JsonConvert.SerializeObject(request));
        stringContent.Headers.Add("Content-Disposition", "form-data; name=\"json\"");
        content.Add(stringContent, "json");

【服务端】如何接收客户端的参数?

        string id=HttpContext.Current.Request["id"];
        string name = HttpContext.Current.Request["name"];

【服务端】如何接收文件做中转?

        HttpContent bytesContent = new ByteArrayContent(paramFileBytes);
        using (var client = new HttpClient())
        using (var formData = new MultipartFormDataContent())
        {
            bytesContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
            {         
                Name = "file", // form value name must be 'file'
                FileName = fileName,
            };
            var response = client.PostAsync(url, formData).Result;
            if (!response.IsSuccessStatusCode)
            {
                return null;
            }
            return response.Content.ReadAsStreamAsync().Result;
        }
上一篇下一篇

猜你喜欢

热点阅读