WebClient.UploadData 出现问题
我有一个客户端-服务器类型的应用程序,其中服务器运行 HttpListener,客户端使用 WebClient.UploadData 将数据上传到服务器。该代码工作得很好(对于大数据缓冲区 60K 及以上),除了一个安装,当数据缓冲区大小大于 16384 时,UploadData 会超时。 这是我在客户端上的代码:
internal bool UploadData(byte[] buffer, String file, String folder)
{
try
{
String uri = "http://" + GlobalData.ServerIP + ":" + GlobalData.ServerHttpPort + "/upload:";
NameValueCollection headers = new NameValueCollection();
headers.Set("Content-Type", "application/octet-stream");
headers.Set("Y-Folder", folder);
headers.Set("Y-File", file);
using (WebClient wc = new WebClient())
{
wc.Credentials = new NetworkCredential(WebUserName, WebPassword);
wc.Headers.Add(headers);
wc.UploadData(new Uri(uri), buffer);
return true;
}
}
catch (Exception ex)
{
GlobalData.ODS("Exception in UploadFile " + ex.Message);
return false;
}
}
在服务器上
ODS(TraceDetailLevel.Level4, "Process upload ");
HttpListenerResponse response = e.RequestContext.Response;
String disp = "";
String fil = "";
String folder = "";
Stream body = e.RequestContext.Request.InputStream;
long len64 = e.RequestContext.Request.ContentLength64;
Encoding encoding = e.RequestContext.Request.ContentEncoding;
ODS(TraceDetailLevel.Level4, "Process upload " + len64 + " bytes encoding " + encoding.EncodingName);
NameValueCollection nvp = e.RequestContext.Request.Headers;
try
{
disp = nvp["Content-Disposition"];
fil = nvp["Y-File"];
folder = nvp["Y-Folder"];
}
catch { }
BinaryReader reader = new BinaryReader(body, encoding);
byte[] data = new byte[len64];
long total = 0;
while (true)
{
int dataleft = data.Length - (int)total;
int offset = (int)total;
GlobalData.ODS("Reading binary stream offset=" + offset + " read dataleft=" + dataleft);
int cnt = reader.Read(data, offset, dataleft);
if (cnt <= 0)
{
break;
}
total += cnt;
if (len64 <= total)
{
break;
}
}
ODS(TraceDetailLevel.Level4, "Process upload: Got data "+total+" should have="+len64);
if (total == len64)
{
//process data
上面的代码在除一个之外的所有系统上都运行良好安装。怎么了?
I have a client-server type application with the server running HttpListener and the client uploading data to the server using WebClient.UploadData. The code works quite well (whith large data buffers 60K and up) except one installation where UploadData times out when data buffer size is bigger that 16384. Here is my code on the client:
internal bool UploadData(byte[] buffer, String file, String folder)
{
try
{
String uri = "http://" + GlobalData.ServerIP + ":" + GlobalData.ServerHttpPort + "/upload:";
NameValueCollection headers = new NameValueCollection();
headers.Set("Content-Type", "application/octet-stream");
headers.Set("Y-Folder", folder);
headers.Set("Y-File", file);
using (WebClient wc = new WebClient())
{
wc.Credentials = new NetworkCredential(WebUserName, WebPassword);
wc.Headers.Add(headers);
wc.UploadData(new Uri(uri), buffer);
return true;
}
}
catch (Exception ex)
{
GlobalData.ODS("Exception in UploadFile " + ex.Message);
return false;
}
}
On the server
ODS(TraceDetailLevel.Level4, "Process upload ");
HttpListenerResponse response = e.RequestContext.Response;
String disp = "";
String fil = "";
String folder = "";
Stream body = e.RequestContext.Request.InputStream;
long len64 = e.RequestContext.Request.ContentLength64;
Encoding encoding = e.RequestContext.Request.ContentEncoding;
ODS(TraceDetailLevel.Level4, "Process upload " + len64 + " bytes encoding " + encoding.EncodingName);
NameValueCollection nvp = e.RequestContext.Request.Headers;
try
{
disp = nvp["Content-Disposition"];
fil = nvp["Y-File"];
folder = nvp["Y-Folder"];
}
catch { }
BinaryReader reader = new BinaryReader(body, encoding);
byte[] data = new byte[len64];
long total = 0;
while (true)
{
int dataleft = data.Length - (int)total;
int offset = (int)total;
GlobalData.ODS("Reading binary stream offset=" + offset + " read dataleft=" + dataleft);
int cnt = reader.Read(data, offset, dataleft);
if (cnt <= 0)
{
break;
}
total += cnt;
if (len64 <= total)
{
break;
}
}
ODS(TraceDetailLevel.Level4, "Process upload: Got data "+total+" should have="+len64);
if (total == len64)
{
//process data
The code above works well on all but one installation. What is wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看来我找到了问题的根源。这个导致我的代码失败的安装在运行我的
HTTP
服务器代码的计算机上安装了 AVG Free Antivirus。如果我在那台计算机上禁用 AVG,我的代码就可以工作。想知道是否有人在使用 AVG 时遇到类似问题。It looks like I found the source of the problem. This one installation in question that fails my code has AVG Free Antivirus installed on a computer that runs my
HTTP
server code. If I disable AVG on that computer my code works. Wondering if anybody runs into similar issues with AVG.