开发者

Stream was not readable - nothing is disposed of, why does this not work?

开发者 https://www.devze.com 2023-02-19 00:30 出处:网络
I have the following code: // Create POST data and convert it to a byte array. string postData = \"name=t&description=tt\";

I have the following code:

 // Create POST data and convert it to a byte array.
 string postData = "name=t&description=tt";
 byte[] byteArray = Encoding.UTF8.GetBytes(postData);
 // Set the ContentType pro开发者_StackOverflowperty of the WebRequest.
 request.ContentType = "application/x-www-form-urlencoded";
 // Set the ContentLength property of the WebRequest.
 request.ContentLength = byteArray.Length;
 // Get the request stream.
 using (StreamReader reader = new StreamReader(request.GetRequestStream()))
 {
     string s = reader.ReadToEnd();
 }

When the flow hits the using (..... ) statement, I get the following exception:

Stream was not readable

I want to read the entire request stream into a string, nothing is closed, so the code should work?


You write to the Request stream and read from the Response stream.

    string postData = "name=t&description=tt";
    byte[] byteArray = Encoding.UTF8.GetBytes(postData);
    // Set the ContentType property of the WebRequest.
    request.ContentType = "application/x-www-form-urlencoded";
    // Set the ContentLength property of the WebRequest.
    request.ContentLength = byteArray.Length;
    // Get the request stream.

    var stream = request.GetRequestStream();
    stream.Write( byteArray, 0, byteArray.Length );


The stream is not readable, because it's intended to be written to. See this link for a simple example:

http://www.netomatix.com/httppostdata.aspx

0

精彩评论

暂无评论...
验证码 换一张
取 消