2010-12-03 3 views
1

私は、RestfulサービスにデータをPOSTしようとしていますが、このエラーが発生しています。どんな助けでも大歓迎です。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

実際に* Positionを使用していないので、上記のコードがエラーを引き起こしたとは思われません。 WebResponseやストリームの 'using'ステートメントも使用していません。 –

+0

最初に使用してみましたが、同じエラーが発生しました。ポジションの使い方を教えていただけますか? – rasi

答えて

-5

ストリームをシーク可能にするには、StreamをMemoryStreamのようなものにキャストする必要があります。 CanSeekがfalseのストリームでは、長さと位置は無効です。この

+0

MemoryStreamへのキャストは失敗します... –

+0

私は修正されました。あなたはdataStream.CopyTo(memStream)のようなものを使うことができます。 – Derek

+0

コードを共有できますか? – rasi

5

重複:information

リードCopsey答え述べによる「のStream.lengthのみシークが利用可能なストリームの実装に取り​​組んでいますあなたは通常Stream.CanSeekが真であるかどうかを確認することができます。」

関連する問題