2

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;
    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
user2836292
  • 305
  • 2
  • 5
  • 13

1 Answers1

3

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.

Theodoros Chatzigiannakis
  • 28,773
  • 8
  • 68
  • 104