2010-10-03 74 views
1

所以我想創建一個簡單的類,我可以使用它來使用REST Web服務。不過,我有一些HttpWebRequest對象的麻煩。這裏是我的代碼:HttpWebResponse的多個問題

using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 

namespace RichardKnop.Utils 
{ 
    public class REST 
    { 
     public void POST(string Uri) 
     { 
     } 

     public void GET(string Uri) 
     { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 

      // Set some reasonable limits on resources used by this request 
      request.MaximumAutomaticRedirections = 4; 
      request.MaximumResponseHeadersLength = 4; 
      // Set credentials to use for this request. 
      request.Credentials = CredentialCache.DefaultCredentials; 
      HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

      Console.WriteLine("Content length is {0}", response.ContentLength); 
      Console.WriteLine("Content type is {0}", response.ContentType); 

      // Get the stream associated with the response. 
      Stream receiveStream = response.GetResponseStream(); 

      // Pipes the stream to a higher level stream reader with the required encoding format. 
      StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8); 

      Console.WriteLine("Response stream received."); 
      Console.WriteLine(readStream.ReadToEnd()); 
      response.Close(); 
      readStream.Close(); 
     } 
    } 
} 

但是我得到幾個錯誤 - 例如:

Error 4 'System.Net.HttpWebRequest' does not contain a definition for 'GetResponse' and no extension method 'GetResponse' accepting a first argument of type 'System.Net.HttpWebRequest' could be found (are you missing a using directive or an assembly reference?) C:\Users\Richard\Documents\Visual Studio 2010\Projects\RichardKnop\RichardKnop\Utils\REST.cs 31 65 RichardKnop 

這怎麼可能,它不包含GetResponse的方法的定義時,我可以清楚地看到它確實有這樣的方法的文檔?那就是:如果

http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.getresponse.aspx

對不起這是一件小事,但我是新來的.NET。

+1

所有大寫方法和類名......這將變得非常快速。 – 2010-10-03 15:13:54

回答

3

據我知道的Silverlight只允許異步調用

嘗試使用BeginGetResponseMSDN link