user2836292
user2836292

Reputation: 305

Stream does not support seek operations

I'm trying to POST to a website where the only parameter needed is "description." I believe it has something to do with the way that I am encoding my data. Am I missing something/going about this the wrong way?

string postData = String.Format("description=" + description + "&");
byte[] byteArray = Encoding.UTF8.GetBytes(postData);

HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.Method = WebRequestMethods.Http.Post;
wr.ContentLength = byteArray.Length;
wr.ContentType = "application/x-www-form-urlencoded";

Stream postStream = wr.GetRequestStream();
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();

WebResponse response = await wr.GetResponseAsync();
HttpWebResponse webResponse = (HttpWebResponse)response;
using (var stm = response.GetResponseStream())
{
    using (var reader = new StreamReader(stm))
    {
        var content = await reader.ReadToEndAsync();
        reader.Close();
        stm.Close();
        return content;
    }
}

Upvotes: 2

Views: 4625

Answers (1)

Theodoros Chatzigiannakis
Theodoros Chatzigiannakis

Reputation: 29233

The stream you are trying to read apparently doesn't support seeking. You can also verify that programmatically through its CanSeek property.

If you want the same data in a stream that you can seek, consider reading the entirety of your current stream to a byte[] buffer, then constructing a MemoryStream out of that buffer for use with your StreamReader.

Upvotes: 3

Related Questions