How do you set up the correct HTTP Response object for a Range request coming from BITS (Background Intelligent Transfer Service)?

╄→гoц情女王★ 提交于 2019-12-08 09:10:26

问题


I have a requirement to implement a web-service that can issue files to the bits (Background Intelligent Transfer Service). The language is ASP.NET (C#). The problem I am having is with the "range" stuff.

My code currently receives the http request (with a valid range is present in the http headers of 0 - 4907), and subsequently dishes out a portion of a byte array in the response object.

Here's my server code:

_context.Response.Clear();
_context.Response.AddHeader("Content-Range", "bytes " + lower.ToString() + "-" +  upper.ToString() + "//" + view.Content.Length.ToString());
_context.Response.AddHeader("Content-Length", upper.ToString());
_context.Response.AddHeader("Accept-Ranges", "bytes");
_context.Response.ContentType = "application/octet-stream";
_context.Response.BinaryWrite(data);
_context.Response.End();

What happens next is that the subsequent request does not have any "range" key in the header at all... it's like it is asking for the entire file! Needless to say, the bits job errors stating that the servers response was not valid.

I suspect that it's all down to the headers that the server is returning in the response object... I am pretty sure that I am following protocol here.

If anyone can help with this it would be greatly appreciated... mean while... I'll keep on searching!

Regards


回答1:


Yes, I found that I had a few issues in total. IIS was an initial problem, then my length calculations... and then like you say, the range request it's self. Ignoring the latter, my final code for this segment was:

_context.Response.StatusCode = 206;
_context.Response.AddHeader("Content-Range", string.Format("bytes {0}-{1}/{2}", lower.ToString(), upper.ToString(), view.Content.Length.ToString()));
_context.Response.AddHeader("Content-Length", length.ToString());
_context.Response.AddHeader("Accept-Ranges", "bytes");
_context.Response.OutputStream.Write(view.Content.ToArray(), lower, length);

Handling multi-request ranges can be tackled a different day! Should BITS request in this way (like it does on the second request after the first request which asks for the entire file), my code simply returns nothing... and then BITS sends a single range in the request... things work ok from there.

Thanks for the response.




回答2:


You could also test-run your BITS request(s) against a known static file and sniff the packets with WireShark. That's sure to reveal exactly how to do it.



来源:https://stackoverflow.com/questions/7527799/how-do-you-set-up-the-correct-http-response-object-for-a-range-request-coming-fr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!