Stream CanRead/CanSeek false + Length&Position 抛出异常 NotSupportedException
Stream CanRead/CanSeek false + Length&Position threw exception NotSupportedException
我是新手,我想做的是 POST 图像作为字节数组到 URL,但是当我尝试获取响应时出现错误。
这是来自 MSDN 的代码:
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(BASE_URL);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
byte[] byteArray = fileContent; // my image as bytearray
// 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.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse(); // here it stops
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
在 Stream 中调试时,CanRead 和 CanSeek 为 false,Length 和 Position 都抛出相同的错误:
'((System.Net.ConnectStream)dataStream).Length' threw an exception of
type 'System.NotSupportedException'.
我尝试复制到内存流,但同样的问题。我在互联网上找到的任何东西都没有帮助我解决这个问题。
关于调试
的正常行为
The NetworkStream does not support random access to the network data
stream. The value of the CanSeek property, which indicates whether the
stream supports seeking, is always false; reading the Position
property, reading the Length property, or calling the Seek method will
throw a NotSupportedException.
关于您的 MSDN 示例
笔记状态
If a WebException is thrown, use the Response and Status properties of
the exception to determine the response from the server.
虽然有很多方法可以使用WebRequest
将其包装在 try catch
中并计算出实际的异常是什么以及它告诉你什么是值得的
try
{
WebResponse response = request.GetResponse();
...
...
}
catch(WebException ex)
{
// inspect Response and Status properties
}
我是新手,我想做的是 POST 图像作为字节数组到 URL,但是当我尝试获取响应时出现错误。
这是来自 MSDN 的代码:
// Create a request using a URL that can receive a post.
WebRequest request = WebRequest.Create(BASE_URL);
// Set the Method property of the request to POST.
request.Method = "POST";
// Create POST data and convert it to a byte array.
byte[] byteArray = fileContent; // my image as bytearray
// 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.
Stream dataStream = request.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
// Get the response.
WebResponse response = request.GetResponse(); // here it stops
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
dataStream = response.GetResponseStream();
// Open the stream using a StreamReader for easy access.
StreamReader reader = new StreamReader(dataStream);
// Read the content.
string responseFromServer = reader.ReadToEnd();
// Display the content.
Console.WriteLine(responseFromServer);
// Clean up the streams.
reader.Close();
dataStream.Close();
response.Close();
在 Stream 中调试时,CanRead 和 CanSeek 为 false,Length 和 Position 都抛出相同的错误:
'((System.Net.ConnectStream)dataStream).Length' threw an exception of type 'System.NotSupportedException'.
我尝试复制到内存流,但同样的问题。我在互联网上找到的任何东西都没有帮助我解决这个问题。
关于调试
的正常行为The NetworkStream does not support random access to the network data stream. The value of the CanSeek property, which indicates whether the stream supports seeking, is always false; reading the Position property, reading the Length property, or calling the Seek method will throw a NotSupportedException.
关于您的 MSDN 示例
笔记状态
If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.
虽然有很多方法可以使用WebRequest
将其包装在 try catch
中并计算出实际的异常是什么以及它告诉你什么是值得的
try
{
WebResponse response = request.GetResponse();
...
...
}
catch(WebException ex)
{
// inspect Response and Status properties
}