使用HttpWebRequest上传大型多个文件

时间:2013-02-27 06:18:21

标签: c# httpwebrequest

我正在使用HttpWebRequest使用Rest API将文件上传到远程服务器。我上传的文件大于300MB。但我有超时例外。如果我设置webRequest.Timeout = 600000,则上传需要很长时间。我希望它更快。

我正在使用以下代码:

using (var stream = File.Open(Path.Combine(folder, fileName), FileMode.Open))
{
    var files = new UploadFile
                    {
                        Name = "file",
                        Filename = Path.Combine(folder, fileName),
                        ContentType = "application/binary",
                        Stream = stream
                    };



    var values = new NameValueCollection
                     {
                         { "fileName", fileName },
                         { "processId", processId },
                         { "document", document },
                     };

    UploadFiles(remoteUrl, files, values);
}

UploadFiles(remoteUrl, files, values);

public static byte[] UploadFiles(string url, UploadFile file, NameValueCollection nvc)
{
    var request = WebRequest.Create(url);
    request.Method = "POST";
    var boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x", NumberFormatInfo.InvariantInfo);
    request.ContentType = "multipart/form-data; boundary=" + boundary;
    boundary = "--" + boundary;

    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
    webRequest.KeepAlive = true;
    webRequest.Timeout = 3600;

    webRequest.SendChunked = true;

    using (var requestStream = request.GetRequestStream())
    {
        // Write the values
        foreach (string name in nvc.Keys)
        {
            var buffer = Encoding.UTF8.GetBytes(boundary + Environment.NewLine);
            requestStream.Write(buffer, 0, buffer.Length);
            buffer =
                Encoding.UTF8.GetBytes(
                    string.Format("Content-Disposition: form-data; name=\"{0}\"{1}{1}", name, Environment.NewLine));
            requestStream.Write(buffer, 0, buffer.Length);
            buffer = Encoding.UTF8.GetBytes(nvc[name] + Environment.NewLine);
            requestStream.Write(buffer, 0, buffer.Length);
        }

        // Write the files


        var trailer = Encoding.UTF8.GetBytes(boundary + Environment.NewLine);
        requestStream.Write(trailer, 0, trailer.Length);
        trailer =
            Encoding.UTF8.GetBytes(
                string.Format(
                    "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"{2}",
                    file.Name,
                    file.Filename,
                    Environment.NewLine));
        requestStream.Write(trailer, 0, trailer.Length);
        trailer = Encoding.UTF8.GetBytes(string.Format("Content-Type: {0}{1}{1}", file.ContentType, Environment.NewLine));
        requestStream.Write(trailer, 0, trailer.Length);
        file.Stream.CopyTo(requestStream);
        trailer = Encoding.UTF8.GetBytes(Environment.NewLine);
        requestStream.Write(trailer, 0, trailer.Length);


        var boundaryBuffer = Encoding.UTF8.GetBytes(boundary + "--");
        requestStream.Write(boundaryBuffer, 0, boundaryBuffer.Length);
    }

    using (var response = request.GetResponse())
    using (var responseStream = response.GetResponseStream())
    using (var stream = new MemoryStream())
    {
        responseStream.CopyTo(stream);
        return stream.ToArray();
    }

}

0 个答案:

没有答案