2010-12-03 29 views
1

我试图将数据发布到Restful服务并获取此错误。任何帮助不胜感激。Stream.Length引发NotSupportedException

长度= '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

上面的代码看起来不太可能会导致该错误,因为您实际上并不使用*位置。请注意,您还没有为您的WebResponse或流使用`using`语句... – 2010-12-03 16:16:24

+0

我尝试使用第一次,但得到相同的错误。你能告诉我如何使用职位吗? – rasi 2010-12-03 16:33:55

回答

-5

您需要将您的流转换为类似一个MemoryStream,以便它是寻求-能。长度和位置在CanSeek为false的流上无效。

5

重复的是:information

里德·科普塞答案通过声明“ Stream.Length仅适用于流实现中寻求的是提供您通常可以检查,看看是否Stream.CanSeek是真的”

相关问题