当前位置: 代码迷 >> VB Dotnet >> request.AddRange为什么不起作用?该怎么处理
  详细解决方案

request.AddRange为什么不起作用?该怎么处理

热度:259   发布时间:2016-04-25 02:23:09.0
request.AddRange为什么不起作用?
本帖最后由 clpwhw 于 2014-03-05 21:09:01 编辑
我想从文件中读取一部分字节,可是不知为什么总是读取了整个文件,请各位赐教:
原文件大小:3452字节
程序如下:

          Dim totalBytes As Integer = 0
        Dim fs1 As New FileStream(Path.GetFileNameWithoutExtension("test") + ".$$", FileMode.Create)
        Try
            Dim rqs As HttpWebRequest
            rqs = CType(HttpWebRequest.Create("http://.../templates/jtjy/skins/default/images/jy_01.gif"), HttpWebRequest)
            rqs.AddRange(0, 2452)
            Dim strm As Stream
            strm = rqs.GetResponse.GetResponseStream
            Dim receiveBytes(512 - 1) As Byte
            Dim readBytes As Integer
            readBytes = strm.Read(receiveBytes, 0, receiveBytes.Length)
            Do While readBytes > 0
                fs1.Write(receiveBytes, 0, readBytes)
                totalBytes += readBytes
                readBytes = strm.Read(receiveBytes, 0, receiveBytes.Length)
            Loop
            strm.Close()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

------解决方案--------------------
HTTP 服务器通过 Accept-Ranges 标头来指示对 Range 标头的支持。 来自支持字节范围的服务器的 Accept-Ranges 标头示例如下:

Accept-Ranges: bytes\r\n\r\n

如果从服务器响应的标头中未收到 Accept-Ranges 标头,则该服务器不支持 Range 标头。 来自不支持范围、但能够识别 Accept-Ranges 标头的服务器的 Accept-Ranges 标头示例如下:

Accept-Ranges: none\r\n\r\n 

http://msdn.microsoft.com/zh-cn/library/7fy67z6d%28v=vs.110%29.aspx

你用fiddler发数据看服务器是否支持Ranges

  
*****************************************************************************
签名档: http://feiyun0112.cnblogs.com/
------解决方案--------------------
看这篇文章
http://www.cnblogs.com/mgen/archive/2012/01/30/2331675.html
说服务器会返回Partial Content状态值才是支持Range

我试了一下,确实如此
"http://www.contoso.com"  返回Partial Content
"http://www.cnblogs.com"  返回OK
  相关解决方案