Итак, я пытаюсь создать простой класс, который я мог бы использовать для использования веб-служб REST. Однако у меня возникли некоторые проблемы с объектом 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.