Stream.Length создает исключение NotSupportedException - PullRequest
1 голос
/ 03 декабря 2010

Я пытаюсь отправить данные в службу Restful и получаю эту ошибку. Любая помощь с благодарностью.

Length = 'dataStream.Length' вызвала исключение типа 'System.NotSupportedException'

Position = 'dataStream.Position' вызвало исключение типа 'System.NotSupportedException'

вот код

[WebMethod]
//public static void Main(string output)
public string webPost()
{
    //HttpWebResponse response = null; 
    string output = null;

    // Create a request using a URL that can receive a post. 
    WebRequest request = WebRequest.Create("https://subscribers");
    request.PreAuthenticate = true;
    // Set the Method property of the request to POST.        
    request.Credentials = new NetworkCredential("userid", "password");
    request.Method = WebRequestMethods.Http.Post;

    string EmailAddress = "test@test1.com";
    string FirstName = "first";
    string LastName = "Last";

    StringBuilder Efulfill = new StringBuilder();

    Efulfill.Append("EmailAddress" + HttpUtility.UrlEncode(EmailAddress));
    Efulfill.Append("FirstName" + HttpUtility.UrlEncode(FirstName));
    Efulfill.Append("LastName" + HttpUtility.UrlEncode(LastName));

    byte[] byteData = Encoding.UTF8.GetBytes(Efulfill.ToString());

    request.ContentType = "application/xml;charset=ISO-8859-1";

    request.ContentLength = byteData.Length;

    // Get the request stream.
    Stream dataStream = request.GetRequestStream();
    // Write the data to the request stream.
    dataStream.Write(byteData, 0, byteData.Length);
    // Close the Stream object.
    dataStream.Close();
    // Get the response.
    WebResponse response = request.GetResponse();
    // 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();
    return output;
}

Ответы [ 2 ]

5 голосов
/ 03 декабря 2010

Дубликат этого: информация

Рид Копси отвечает, заявляя: «Stream.Length работает только в реализациях Stream, где поиск доступен. Обычно вы можете проверить, является ли Stream.CanSeek истинным.»

0 голосов
/ 03 декабря 2010

Вам нужно будет привести ваш Stream к чему-то вроде MemoryStream, чтобы он был доступен для поиска.Длина и позиция недопустимы в потоках, где CanSeek имеет значение false.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...