我試圖將數據發佈到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;
}
上面的代碼看起來不太可能會導致該錯誤,因爲您實際上並不使用*位置。請注意,您還沒有爲您的WebResponse或流使用`using`語句... – 2010-12-03 16:16:24
我嘗試使用第一次,但得到相同的錯誤。你能告訴我如何使用職位嗎? – rasi 2010-12-03 16:33:55