使用httpwebrequest下载异步文件时添加空字符 [英] Async file download with httpwebrequest add null characters

查看:91
本文介绍了使用httpwebrequest下载异步文件时添加空字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用MSDN示例生成一个用于从多个网站下载文件的类。我异步获取响应并异步读取响应流到内存流中。当EndRead返回0时,我调用memoryStream.ToArray()并通过FileStream将其保存到文件中。

应用程序通过实例化上述许多对象同时下载多个文件。大部分时间都可以正常工作,但在某些情况下,某些文件最终会在文件末尾带有NULL字符(并不总是相同数量)。

我做了一些跟踪我目前只看到Apache服务器发生过这种情况。我也怀疑它与Transfer-Encoding有关:chunked,但尚未完全确定。

有什么想法吗?附上了一些源代码。

private void ResponseCallback(IAsyncResult asyncResult)
{
试试
{
Response =(HttpWebResponse)Request.EndGetResponse(asyncResult) );; ResponseStream = Response.GetResponseStream();
ResponseStream.BeginRead(DataBuffer,0,BufferSize,ReadCallback,asyncResult.AsyncState);
} catch(异常e)
{
OnFileDownloadCompleted(e,false,UserState);
}
}

private void ReadCallback(IAsyncResult asyncResult)
{{br / > try
{
if(DataStream == null)
{
DataStream = new MemoryStream();
}

int bytesRead = ResponseStream.EndRead(asyncResult);
if if(bytesRead> 0)
{
DataStream.Write(DataBuffer,0,bytesRead);
ResponseStream.BeginRead(DataBuffer,0, BufferSize,ReadCallback,asyncResult.As yncState);
}其他
{
使用(FileStream fileStream = new FileStream(LocalFileName,FileMode.Create,FileAccess.Write))
{
byte [] data = DataStream.ToArray();
Console.WriteLine(" File" + LocalFileName +"有" + Response.ContentLength +" =" + data.Length +" IS" +(Response.ContentLength == data.Length));
fileStream.Write(data,0,data.Length);
fileStream.Close();
}

File.SetLastWriteTime(LocalFileName,Response.LastModified);
OnFileDownloadCompleted(null,true,UserState);
}}}

抓住(例外e)
{
OnFileDownloadCompleted(e,false,UserState);
}




提前致谢。

费尔南多

Hi,

I have used the MSDN example to generate a class that is used to download files from several web sites. I am asynchronously getting the response and asynchronously reading from the responsestream into a memory stream. When the EndRead returns 0 I call memoryStream.ToArray() and I save that into a file through a FileStream.

The application downloads several files at the same time by instantiating many of the above mentioned objects. All works fine most of the times, but in some ocations some of the files end up with NULL characters at the end of the file (not always the same number of them).

I have done some tracing and I've only seen it happening with Apache servers at the moment. I am also suspect it has something to do with Transfer-Encoding: chunked, but not completely sure yet.

Any Ideas? Have attached some source code.

  private void ResponseCallback(IAsyncResult asyncResult)
  {
   try
   {
    Response = (HttpWebResponse)Request.EndGetResponse(asyncResult);
    ResponseStream = Response.GetResponseStream();
    ResponseStream.BeginRead(DataBuffer, 0, BufferSize, ReadCallback, asyncResult.AsyncState);
   }
   catch (Exception e)
   {
    OnFileDownloadCompleted(e, false, UserState);
   }
  }

  private void ReadCallback(IAsyncResult asyncResult)
  {
   try
   {
    if (DataStream == null)
    {
     DataStream = new MemoryStream();
    }
    
    int bytesRead = ResponseStream.EndRead(asyncResult);
    if (bytesRead > 0)
    {
     DataStream.Write(DataBuffer, 0, bytesRead);
     ResponseStream.BeginRead(DataBuffer, 0, BufferSize, ReadCallback, asyncResult.AsyncState);
    }
    else
    {
     using (FileStream fileStream = new FileStream(LocalFileName, FileMode.Create, FileAccess.Write))
     {
                        byte[] data = DataStream.ToArray();
                        Console.WriteLine("File " + LocalFileName + " has " + Response.ContentLength + " = " + data.Length + " IS " + (Response.ContentLength == data.Length));
      fileStream.Write(data, 0, data.Length);
      fileStream.Close();
     }
     
     File.SetLastWriteTime(LocalFileName, Response.LastModified);
     OnFileDownloadCompleted(null, true, UserState);
    }
   }
   catch (Exception e)
   {
    OnFileDownloadCompleted(e, false, UserState);
   }
  }


Thanks in advance.

Fernando

推荐答案

嗯...什么是DataBuffer和BufferSize?它们似乎没有在这里定义。如果可能的话,一个小但可编辑的问题示例将极大地帮助我们。

Hmmmm... What's DataBuffer and BufferSize? They don't seem to be defined here.  A small, but compilable example of the problem, if possible, would greatly help us help you.


这篇关于使用httpwebrequest下载异步文件时添加空字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆