C# 上传速度问题:HttpWebRequest
我正在使用HttpWebRequest将文件上载到某个服务器,现在的问题是速度问题 我无法获得与浏览器(Mozilla Firefox)相同的上传速度,我获得的速度是浏览器速度的1/5 以下是我的HttpWebRequest对象的设置C# 上传速度问题:HttpWebRequest,c#,.net,upload,httpwebrequest,C#,.net,Upload,Httpwebrequest,我正在使用HttpWebRequest将文件上载到某个服务器,现在的问题是速度问题 我无法获得与浏览器(Mozilla Firefox)相同的上传速度,我获得的速度是浏览器速度的1/5 以下是我的HttpWebRequest对象的设置 //headers is a NameValueCollection type object, //Method is a struct { GET, POST, HEAD } HttpWebRequest request = (HttpWebR
//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;
我一直启用的一些全局选项
ServicePointManager.Expect100Continue = false;
ServicePointManager.DefaultConnectionLimit = 200;
ServicePointManager.MaxServicePointIdleTime = 2000;
ServicePointManager.MaxServicePoints = 1000;
ServicePointManager.SetTcpKeepAlive(false, 0, 0);
我是如何发送文件的块
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);
}
}
if(PostMethod==PostType.MultiPart&&uploadFiles.Count>0)
{
对于(int i=0;i0)
{
writer.Write(缓冲区,0,fileBytesRead);
bSent+=fileBytesRead;
int timeNow=Environment.TickCount;
如果(timeNow-lastTime>=500)
{
lastTime=timeNow;
参数进度=(int)(bSent*100/总字节);
参数速度=(bSent/sw.passed.TotalSeconds);
进展(arg);
}
}
tmpBytes=Encoding.Default.GetBytes(“\r\n”);
writer.Write(tmpBytes,0,tmpBytes.Length);
bSent+=tmpBytes.长度;
参数进度=(int)(bSent*100/总字节);
参数速度=(bSent/sw.passed.TotalSeconds);
进展(arg);
}
}
我很高兴能达到浏览器上传速度的75%。我通过搜索这个网站找到了答案。它似乎是一个复制品 从张贴在那里的答案来看: “第一次请求页面时,.net尝试检测代理设置。解决方案是传入一个空的WebProxy对象。这样,它只连接到远程服务器,而不是自动检测代理服务器。”- 尝试添加
request.Proxy = new WebProxy();
以下是我们可以提高上传速度的各种选项:- 在大型文件的配置文件中使用此选项:--- 在web config system.web> httpRuntime maxRequestLength=“2097151” 这可能有助于在IIS 7.0中找到解决方案 将其写入web config system.web>httpModules
**add name="ITHitPutUploadProgressAndResumeModule" type="ITHit.WebDAV.Server.ResumableUpload.PutUploadProgressAndResumeModule, ITHit.WebDAV.Server"**
两件事
来自MSDN:与Firefox相比,该浏览器是否有任何可能影响浏览器速度的附加组件。我有一个同事说他们在那里的浏览器上传速度慢,让em使用Windows默认地址导航,而且上传速度快。原来他们有一个插件,通过虹吸的数据,并造成了巨大的延迟。我会努力想出更好的办法。@Greg-不,我只有两个插件,livehttp头和篡改数据!有一些吗