HttpWebResponse存在多个问题

时间:2010-10-03 13:28:44

标签: c# silverlight rest

所以我试图创建一个可以用来使用REST Web服务的简单类。但是我遇到了一些HttpWebRequest对象的麻烦。这是我的代码:

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 个答案:

答案 0 :(得分:3)

据我所知,Silverlight只允许异步调用

尝试使用BeginGetResponseMSDN link