序列化的最佳选择是"HEAD".要求 [英] Best choice in serialization, "HEAD" requests

查看:60
本文介绍了序列化的最佳选择是"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???????

推荐答案

希望您可以决定对自己有什么好处

要求响应与将与GET请求对应的响应相同,但没有响应主体.这对于检索写在响应头中的元信息很有用,而不必传输整个内容.
获取
请求指定资源的表示形式.使用GET(和其他一些HTTP方法)的请求除了进行检索之外,没有其他作用".

XML序列化注意事项

使用XmlSerialzer类应考虑以下内容:

1.不包括类型标识和程序集信息.换句话说,XML序列化不能保持类型保真度.要保持类型保真度,请改用二进制序列化.
2.只有公共属性和字段可以序列化.要序列化非公共数据,请使用二进制序列化.
3.一个类必须具有默认的构造函数,才能使用XmlSerialzer类进行序列化.
4.方法无法序列化.
如果满足特定要求,则XmlSerialzer类可以对实现IColleciton和IEnumerable的类进行序列化.
Hope you can decide what is good for you
HEAD
Asks for the response identical to the one that would correspond to a GET request, but without the response body. This is useful for retrieving meta-information written in response headers, without having to transport the entire content.
GET
Requests a representation of the specified resource. Requests using GET (and a few other HTTP methods) "SHOULD NOT have the significance of taking an action other than retrieval".

XML Serialization Considerations

The following should be considered using XmlSerialzer class:

1. Type identity and assembly information is not included. In other words, XML serialization does not maintain type fidelity. To maintain type fidelity use binary serialization instead.
2. Only public properties and fields can be serialized. To serialize non-public data use binary serialization instead.
3. A class must have a default constructor to be serialized with the XmlSerialzer class.
4. Methods cannot be serialized.
XmlSerialzer class can serialize classes that implement IColleciton and IEnumerable differently if they meet certain requirements.


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

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