上传速度问题:HttpWebRequest

PaR*_*RaJ 14 .net c# upload httpwebrequest

我正在使用HttpWebRequest将文件上传到某个服务器,现在问题是我有速度问题.

我无法获得与浏览器(Mozilla Firefox)相同的上传速度,我获得的速度是我从浏览器获得的速度的1/5.

这是我的HttpWebRequest对象的设置

//headers is a NameValueCollection type object,
//Method is a struct { GET, POST, HEAD }

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
        request.UserAgent = headers["User-Agent"];
        request.KeepAlive = false;
        request.Accept = headers["Accept"];
        request.AllowAutoRedirect = AllowRedirect;
        request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US,en;q=0.5");
        request.Method = Method.ToString();
        request.AllowWriteStreamBuffering = false;
        request.ReadWriteTimeout = 60000;
Run Code Online (Sandbox Code Playgroud)

我保持启用的一些全局选项

        ServicePointManager.Expect100Continue = false;
        ServicePointManager.DefaultConnectionLimit = 200;
        ServicePointManager.MaxServicePointIdleTime = 2000;
        ServicePointManager.MaxServicePoints = 1000;
        ServicePointManager.SetTcpKeepAlive(false, 0, 0);
Run Code Online (Sandbox Code Playgroud)

我如何以块发送文件...

            if (PostMethod == PostType.MultiPart && uploadFiles.Count > 0)
            {
                for (int i = 0; i < uploadFiles.Count; i++)
                {
                    string fileParam = uploadFiles.GetKey(i);
                    string tmpFilename = uploadFiles.Get(i);
                    string tmpData =
                    string.Format(
                        "--{0}\r\nContent-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"\r\nContent-Type: {3}\r\n\r\n", boundary, fileParam, Path.GetFileName(tmpFilename), MimeType.GetMimeByExtension(Path.GetExtension(tmpFilename)));
                    byte[] tmpBytes = Encoding.Default.GetBytes(tmpData);
                    writer.Write(tmpBytes, 0, tmpBytes.Length);
                    bSent += tmpBytes.Length;

                    arg.Progress = (int)(bSent * 100 / totalBytes);
                    arg.Speed = (bSent / sw.Elapsed.TotalSeconds);
                    OnProgress(arg);

                    //write the file
                    int fileBytesRead;

                    FileStream fileStream = File.Open(tmpFilename, FileMode.Open, FileAccess.Read, FileShare.Read);
// buffer size = 8192
                    while ((fileBytesRead = fileStream.Read(buffer, 0, BUFFER_SIZE)) > 0) 

                    {
                        writer.Write(buffer, 0, fileBytesRead);
                        bSent += fileBytesRead;

                        int timeNow = Environment.TickCount;
                        if (timeNow - lastTime >= 500)
                        {
                            lastTime = timeNow;
                            arg.Progress = (int)(bSent * 100 / totalBytes);
                            arg.Speed = (bSent / sw.Elapsed.TotalSeconds);
                            OnProgress(arg);
                        }
                    }

                    tmpBytes = Encoding.Default.GetBytes("\r\n");
                    writer.Write(tmpBytes, 0, tmpBytes.Length);
                    bSent += tmpBytes.Length;

                    arg.Progress = (int)(bSent * 100 / totalBytes);
                    arg.Speed = (bSent / sw.Elapsed.TotalSeconds);
                    OnProgress(arg);

                }
            }
Run Code Online (Sandbox Code Playgroud)

任何帮助都非常受欢迎,甚至可以帮助我获得75%的浏览器上传速度.

Sha*_*ook 6

我通过搜索这个网站找到了答案.它似乎是".NET HttpWebRequest Speed vs. Browser"的副本.

从那里发布的答案:

"第一次请求页面时,.NET尝试检测代理设置.解决的办法是在一个空WebProxy对象通过.这样,它只是连接到远程服务器,而不是自动探测代理服务器." - 马科斯

尝试添加

    request.Proxy = new WebProxy();
Run Code Online (Sandbox Code Playgroud)