I have such piece of code:
var uri = "myURL.com"
var request = (HttpWebRequest)WebRequest.Create(uri);
string postData = "myData";
byte[] data = Encoding.UTF8.GetBytes(postData);
request.Method = "POST";
request.UseDefaultCredentials = true;
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
request.AddRange(1024);
request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.82 Safari/537.36";
Stream stream = request.GetRequestStream();
stream.Write(data, 0, data.Length);
WebResponse response = request.GetResponse();
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
string responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
Console.WriteLine(responseString);
response.Close();
stream.Close();
And I get an exception like: "This stream does not support seek operations". This error occurs in stream.Length
and stream.Position
. And I although think, that because of this error my postData
doesn't sent to the server.
Here is the screenshot of the exception
This exception is thrown only when you call a method or a property that not compatible with the current stream (NetworkStream in your case). If you need to move backward you need to copy the content in a temporary stream (MemoryStream, FileStream, ...).
Your sample code doesn't have issue with this scenario. The exception you can see in Visual studio is because VS try to access to each property to display a value. When your code run, properties like 'Position' are not called and everything is fine.
To programmatically know if you can seek in a stream. use the property CanSeek
of the stream.