如何从C#提交multipart / form-data HTTP POST请求

时间:2009-07-30 00:24:46

标签: c# .net http post multipartform-data

使用C#中的multipart / form-data内容类型提交HTTP POST请求的最简单方法是什么?必须有一个比建立我自己的请求更好的方法。

我要问的原因是使用此API将照片上传到Flickr:

http://www.flickr.com/services/api/upload.api.html

6 个答案:

答案 0 :(得分:5)

如果您使用的是.NET 4.5,请使用:

public string Upload(string url, NameValueCollection requestParameters, MemoryStream file)
        {

            var client = new HttpClient();
            var content = new MultipartFormDataContent();

            content.Add(new StreamContent(file));
            System.Collections.Generic.List<System.Collections.Generic.KeyValuePair<string, string>> b = new List<KeyValuePair<string, string>>();
            b.Add(requestParameters);
            var addMe = new FormUrlEncodedContent(b);

            content.Add(addMe);
            var result = client.PostAsync(url, content);
            return result.Result.ToString();
        }

否则根据Ryan的回答,我下载了库并稍微调整了一下。

  public class MimePart
        {
            NameValueCollection _headers = new NameValueCollection();
            byte[] _header;

            public NameValueCollection Headers
            {
                get { return _headers; }
            }

            public byte[] Header
            {
                get { return _header; }
            }

            public long GenerateHeaderFooterData(string boundary)
            {
                StringBuilder sb = new StringBuilder();

                sb.Append("--");
                sb.Append(boundary);
                sb.AppendLine();
                foreach (string key in _headers.AllKeys)
                {
                    sb.Append(key);
                    sb.Append(": ");
                    sb.AppendLine(_headers[key]);
                }
                sb.AppendLine();

                _header = Encoding.UTF8.GetBytes(sb.ToString());

                return _header.Length + Data.Length + 2;
            }

            public Stream Data { get; set; }
        }

        public string Upload(string url, NameValueCollection requestParameters, params MemoryStream[] files)
        {
            using (WebClient req = new WebClient())
            {
                List<MimePart> mimeParts = new List<MimePart>();

                try
                {
                    foreach (string key in requestParameters.AllKeys)
                    {
                        MimePart part = new MimePart();

                        part.Headers["Content-Disposition"] = "form-data; name=\"" + key + "\"";
                        part.Data = new MemoryStream(Encoding.UTF8.GetBytes(requestParameters[key]));

                        mimeParts.Add(part);
                    }

                    int nameIndex = 0;

                    foreach (MemoryStream file in files)
                    {
                        MimePart part = new MimePart();
                        string fieldName = "file" + nameIndex++;

                        part.Headers["Content-Disposition"] = "form-data; name=\"" + fieldName + "\"; filename=\"" + fieldName + "\"";
                        part.Headers["Content-Type"] = "application/octet-stream";

                        part.Data = file;

                        mimeParts.Add(part);
                    }

                    string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
                    req.Headers.Add(HttpRequestHeader.ContentType, "multipart/form-data; boundary=" + boundary);

                    long contentLength = 0;

                    byte[] _footer = Encoding.UTF8.GetBytes("--" + boundary + "--\r\n");

                    foreach (MimePart part in mimeParts)
                    {
                        contentLength += part.GenerateHeaderFooterData(boundary);
                    }

                    //req.ContentLength = contentLength + _footer.Length;

                    byte[] buffer = new byte[8192];
                    byte[] afterFile = Encoding.UTF8.GetBytes("\r\n");
                    int read;

                    using (MemoryStream s = new MemoryStream())
                    {
                        foreach (MimePart part in mimeParts)
                        {
                            s.Write(part.Header, 0, part.Header.Length);

                            while ((read = part.Data.Read(buffer, 0, buffer.Length)) > 0)
                                s.Write(buffer, 0, read);

                            part.Data.Dispose();

                            s.Write(afterFile, 0, afterFile.Length);
                        }

                        s.Write(_footer, 0, _footer.Length);
                        byte[] responseBytes = req.UploadData(url, s.ToArray());
                        string responseString = Encoding.UTF8.GetString(responseBytes);
                        return responseString;
                    }
                }
                catch
                {
                    foreach (MimePart part in mimeParts)
                        if (part.Data != null)
                            part.Data.Dispose();

                    throw;
                }
            }
        }

答案 1 :(得分:3)

我自己没有试过这个,但是C#中似乎有一种内置的方式(虽然显然不是一个非常知名的......):

private static HttpClient _client = null;

private static void UploadDocument()
{
    // Add test file 
    var httpContent = new MultipartFormDataContent();
    var fileContent = new ByteArrayContent(File.ReadAllBytes(@"File.jpg"));
    fileContent.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
    {
        FileName = "File.jpg"
    };

    httpContent.Add(fileContent);
    string requestEndpoint = "api/Post";

    var response = _client.PostAsync(requestEndpoint, httpContent).Result;

    if (response.IsSuccessStatusCode)
    {
        // ...
    }
    else
    {
        // Check response.StatusCode, response.ReasonPhrase
    }
}

尝试一下,让我知道它是怎么回事。

干杯!

答案 2 :(得分:2)

我在aspnetupload.com发布的代码取得了成功。我最终创建了自己的UploadHelper库版本,该库与Compact Framework兼容。效果很好,似乎完全符合你的要求。

答案 3 :(得分:1)

首先,使用.Net框架纯手动实现HTTP命令没有任何问题。请记住,它是一个框架,它应该是非常通用的。

其次,我认为您可以尝试在.Net中搜索浏览器实现。我看到了this one,也许它涵盖了你所问的问题。或者您只需搜索“C# http put get post request”即可。其中一个结果导致非自由库可能有用(Chilkat Http)

如果您碰巧在.Net上编写自己的HTTP命令框架 - 我想如果您分享它们我们都会喜欢它: - )

答案 4 :(得分:1)

System.Net.WebClient类可能就是您要查找的内容。检查WebClient.UploadFile的文档,它应该允许您通过UploadFile重载之一上传文件到指定的资源。我认为这是您希望用于发布数据的方法......

它可以像......一样使用。注意这只是未经测试的示例代码...

WebClient webClient = new WebClient();

webClient.UploadFile(“http://www.url.com/ReceiveUploadedFile.aspx”,“POST”,@“c:\ myfile.txt”);

如果您有兴趣,这是MSDN参考。

http://msdn.microsoft.com/en-us/library/system.net.webclient.uploadfile.aspx

希望这有帮助。

答案 5 :(得分:-1)

我通常认为Fiddler是该工作的最佳工具。创建请求非常容易,甚至可以为您生成一些标题。

Fiddler - How to create a request

enter image description here