簡體   English   中英

WebClient.UploadData出現問題

[英]Trouble with 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

上面的代碼在除一種安裝之外的所有安裝上均能正常運行。 怎么了?

看來我找到了問題的根源。 有問題的此安裝使我的代碼失敗,並且在運行我的HTTP服務器代碼的計算機上安裝了AVG Free Antivirus。 如果我在那台計算機上禁用了AVG,則我的代碼有效。 想知道是否有人遇到AVG的類似問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM