本文介绍了序列化的最佳选择是"HEAD".要求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!
我正在C#上创建一个多段下载程序
我有两个简单的问题:

1)我想检索远程资源的长度(文件,html,..)
我可以通过两种方式做到这一点:

Hi guys!
I''m creating a multi - segment download program on C#
and I have TWO simple questions:

1) I want to retrieve the length of the remote resource(file, html, ..)
I can do it in two ways:

HttpWebResponse response;
HttpWebRequest request;
long length;

request = WebRequest.Create(URL) as HttpWebRequest;

// "HEAD" request to extract only headers
request.Method = "HEAD";
 try {
   response = request.GetResponse() as HttpWebResponse;
   length = long.Parse(response.Headers["Content-Length"]);
 } catch(Exception ex) {
     Debug.WriteLine(ex.Message, ex.GetType().ToString());
     throw;
 }
  return length;



或带有"GET"请求



OR with "GET" request

HttpWebResponse response;
HttpWebRequest request;
long length;

request = WebRequest.Create(URL) as HttpWebRequest;


request.Method = "GET";
 try {
   response = request.GetResponse() as HttpWebResponse;
   length = long.Parse(response.Headers["Content-Length"]);
 } catch(Exception ex) {
     Debug.WriteLine(ex.Message, ex.GetType().ToString());
     throw;
 }
  return length;


您如何看待更有效的方法???大多数服务器都支持"HEAD"请求吗?
性能提升了什么?

2)我也有一个关于将下载元数据保存到磁盘的问题
我有一个 Downloader 主类,它封装了一个下载
Segment 类,代表单个下载段(每个
Downloader 包含 Segment 的列表).
细分包含有关开始位置结束位置
的数据 到远程文件的偏移量,并且它还包含本地临时文件的完整路径,该文件用于放置远程资源中的内容...
例如,有一个大小为100MB的文件...
我的程序自动计算分段范围,偏移量,因此每个分段大约有20MB..
我将下载元数据保存在XML文件中,如下所示:


How do you thinks what works FASTER??? Do most servers support "HEAD" request???
What''s the performance boost??

2) I have also a question about saving downloads metadata to disk
I have the main class Downloader which encapsulates a single download
and a Segment class which represents a single segment of download (Each
Downloader contains a list of Segment).
Segment contains data about Start Position, End Position
offsets to the remote file and it also contains a full path to a local temporary file which is used to put content from remote resource...
Example, there is a file that is 100MB in size...
My program automatically calculates segments range, offsets, so each segment would have approximately 20MB..
I save the download metadata in XML file like this:

  <?xml version="1.0"?>
<Download xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="ca817bf2-b62e-4361-8606-b4054f40ec07">
  <URL>http://localhost/drummer_boy.mp3</URL>
  <Rating>8</Rating>
  <Segments>
    <Segment ID="3b928fd7-a097-4508-8b25-1a3220fc078e">
      <SegmentIndex>1</SegmentIndex>
      <URL>http://localhost/drummer_boy.mp3</URL>
      <StartPosition>0</StartPosition>
      <EndPosition>1278540</EndPosition>
      <BytesDownloaded>0</BytesDownloaded>
      <CurrentState>PreparingLocalResources</CurrentState>
      <LocalFilePath xsi:nil="true" />
      <Credentials xsi:nil="true" />
    </Segment>
    <Segment ID="3ded501c-6814-4c93-9dac-b5b9285730c8">
      <SegmentIndex>2</SegmentIndex>
      <URL>http://localhost/drummer_boy.mp3</URL>
      <StartPosition>1278541</StartPosition>
      <EndPosition>2557081</EndPosition>
      <BytesDownloaded>0</BytesDownloaded>
      <CurrentState>PreparingLocalResources</CurrentState>
      <LocalFilePath xsi:nil="true" />
      <Credentials xsi:nil="true" />
    </Segment>
    <Segment ID="97a64fea-c2f3-473d-896b-902e7ebaae43">
      <SegmentIndex>3</SegmentIndex>
      <URL>http://localhost/drummer_boy.mp3</URL>
      <StartPosition>2557082</StartPosition>
      <EndPosition>3835620</EndPosition>
      <BytesDownloaded>0</BytesDownloaded>
      <CurrentState>PreparingLocalResources</CurrentState>
      <LocalFilePath xsi:nil="true" />
      <Credentials xsi:nil="true" />
    </Segment>
    <Segment ID="f54aad7b-6dac-491c-8662-4760ddf7b040">
      <SegmentIndex>5</SegmentIndex>
      <URL>http://localhost/drummer_boy.mp3</URL>
      <StartPosition>0</StartPosition>
      <EndPosition>2000</EndPosition>
      <BytesDownloaded>332</BytesDownloaded>
      <CurrentState>Paused</CurrentState>
      <LocalFilePath xsi:nil="true" />
      <Credentials>
        <Username>Nick</Username>
        <Password>pass</Password>
      </Credentials>
    </Segment>
    <Segment ID="de3aa8df-edb4-407e-9c99-d00646911b1f">
      <SegmentIndex>7</SegmentIndex>
      <URL>http://localhost/drummer_boy.mp3</URL>
      <StartPosition>4554</StartPosition>
      <EndPosition>20000</EndPosition>
      <BytesDownloaded>664</BytesDownloaded>
      <CurrentState>PreparingLocalResources</CurrentState>
      <LocalFilePath xsi:nil="true" />
      <Credentials>
        <Username>Nick</Username>
        <Password>pass</Password>
      </Credentials>
    </Segment>
  </Segments>
</Download>


这些数字非常近似,仅用于测试..
您如何看待...将数据保存在XML文件中是很好的选择,还是我应该使用
我的类的二进制序列化...我认为程序的用户可能有意或无意地破坏了XML文件,这会弄乱了DESERIALIZATION ....您如何看待更好的???????


The numbers are pretty approximate and used just for test..
How do you think...is it a GOOD CHOICE to save data in XML files or I should use
binary serialization of my classes... I think that a user of my program can intentionally or occasionally corrupt XML file and it would mess up the DESERIALIZATION.... how do you think what is BETTER???????

推荐答案


这篇关于序列化的最佳选择是"HEAD".要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 16:02