Error “This stream does not support seek operations” in C#

后端 未结 7 756
梦毁少年i
梦毁少年i 2020-12-01 07:36

I\'m trying to get an image from an url using a byte stream. But i get this error message:

This stream does not support seek operations.<

相关标签:
7条回答
  • 2020-12-01 07:40

    You can't reliably ask an HTTP connection for its length. It's possible to get the server to send you the length in advance, but (a) that header is often missing and (b) it's not guaranteed to be correct.

    Instead you should:

    1. Create a fixed-length byte[] that you pass to the Stream.Read method
    2. Create a List<byte>
    3. After each read, call List.AddRange to append the contents of your fixed-length buffer onto your byte list

    Note that the last call to Read will return fewer than the full number of bytes you asked for. Make sure you only append that number of bytes onto your List<byte> and not the whole byte[], or you'll get garbage at the end of your list.

    0 讨论(0)
  • 2020-12-01 07:41

    Perhaps you should use the System.Net.WebClient API. If already using client.OpenRead(url) use client.DownloadData(url)

    var client = new System.Net.WebClient();
    byte[] buffer = client.DownloadData(url);
    using (var stream = new MemoryStream(buffer))
    {
        ... your code using the stream ...
    }
    

    Obviously this downloads everything before the Stream is created, so it may defeat the purpose of using a Stream. webClient.DownloadData("https://your.url") gets a byte array which you can then turn into a MemoryStream.

    0 讨论(0)
  • 2020-12-01 07:43

    The length of a stream can not be read from the stream since the receiver does not know how many bytes the sender will send. Try to put a protocol on top of http and send i.e. the length as first item in the stream.

    0 讨论(0)
  • 2020-12-01 07:47

    Use a StreamReader instead:

    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
    WebResponse myResp = myReq.GetResponse();
    
    StreamReader reader = new StreamReader(myResp.GetResponseStream());
    return reader.ReadToEnd();
    

    (Note - the above returns a String instead of a byte array)

    0 讨论(0)
  • 2020-12-01 07:50

    You probably want something like this. Either checking the length fails, or the BinaryReader is doing seeks behind the scenes.

    HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url);
    WebResponse myResp = myReq.GetResponse();
    
    byte[] b = null;
    using( Stream stream = myResp.GetResponseStream() )
    using( MemoryStream ms = new MemoryStream() )
    {
      int count = 0;
      do
      {
        byte[] buf = new byte[1024];
        count = stream.Read(buf, 0, 1024);
        ms.Write(buf, 0, count);
      } while(stream.CanRead && count > 0);
      b = ms.ToArray();
    }
    

    edit:

    I checked using reflector, and it is the call to stream.Length that fails. GetResponseStream returns a ConnectStream, and the Length property on that class throws the exception that you saw. As other posters mentioned, you cannot reliably get the length of a HTTP response, so that makes sense.

    0 讨论(0)
  • 2020-12-01 07:51

    With images, you don't need to read the number of bytes at all. Just do this:

    Image img = null;
    string path = "http://www.example.com/image.jpg";
    WebRequest request = WebRequest.Create(path);
    req.Credentials = CredentialCache.DefaultCredentials; // in case your URL has Windows auth
    WebResponse resp = req.GetResponse();
    
    using( Stream stream = response.GetResponseStream() ) 
    {
        img = Image.FromStream(stream);
        // then use the image
    }
    
    0 讨论(0)
提交回复
热议问题