2010-12-03 3 views
1

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

Length = 'dataStream.Length' бросил исключение типа '' System.NotSupportedException

Позиция = '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 = "[email protected]"; 
    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

Похоже, что приведенный выше код вызвал эту ошибку, поскольку вы на самом деле не используете * позицию. Имейте в виду, что вы также не используете выражения `using` для вашего WebResponse или потоков ... – 2010-12-03 16:16:24

+0

Я попытался использовать сначала, но получить ту же ошибку. Можете ли вы сказать мне, как использовать позицию, пожалуйста? – rasi 2010-12-03 16:33:55

ответ

-5

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

5

Дубликат этого: information

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

Смежные вопросы