0

我正在尝试使用 HttpClient 发布多部分/表单数据

该表格需要一定数量的图像。

代码 :

var client = new System.Net.Http.HttpClient();
var content = new MultipartFormDataContent();
var postData = new List<KeyValuePair(string,string)> ();
postData.Add(new KeyValuePair < string, string > ("function", "picture2"));
postData.Add(new KeyValuePair < string, string > ("username ", UserID));
postData.Add(new KeyValuePair < string, string > ("password ", Password));
foreach(var keyValuePair in postData) {
content.Add(new StringContent(keyValuePair.Value),
    String.Format("\"{0}\"", keyValuePair.Key));
}
int x = 1;
foreach(Bitmap item in newpics) {
using(MemoryStream ms = new MemoryStream()) {
    item.Save(ms, ImageFormat.Bmp);
    byte[] bits = ms.ToArray();
    content.Add(new ByteArrayContent(bits), '"' + "pict" + x + '"');
    x += 1;
}
}

问题是只交付了最后一张图片!

为什么会这样??我错过了什么?以及如何解决这个问题?

提前致谢..

4

1 回答 1

0

这是一个如何使用 MultipartFormDataContent 使用 HTTPClient 发布字符串和文件流的示例。需要为每个 HTTPContent 指定 Content-Disposition 和 Content-Type:

这是我的例子。希望能帮助到你:

var path = @"C:\B2BAssetRoot\files\596086\596086.1.mp4";

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

            using (var content = new MultipartFormDataContent())
            {
                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();
于 2015-01-30T18:32:43.993 回答