views:

30

answers:

1

So I am trying to create a simple class which I could use to consume REST web services. However I am having some troubles with HttpWebRequest object. Here is my code:

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();
        }
    }
}

However I am getting several errors - for example:

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

How is that possible that it does not contain definition of the GetResponse method when I can clearly see in the documentation that it does have a method like that? Here it is:

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

Sorry if this is something trivial but I am new to .NET.

+2  A: 

As far as i know Silverlight allows only asynchronous calls

try using BeginGetResponse. MSDN link

Vinay B R