我有一个客户端-服务器类型的应用程序,服务器运行 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
上面的代码适用于所有安装,但只有一个安装。怎么了?