This code comes from Microsoft's documentation. I put this code in a Console app and a Windows Form app separately.
In the Console app, there is an error : “WebRequest does not contain a definition for GetRespone and …”
But in the Windows Form app, there is no error.
I really don't know why this happen. I am a beginner of C#, so this question may be stupid. But I feel very confused. Please explain to me. Thank you!
Below are two screenshots for these two situation:
Here is the code.
using System;
using System.IO;
using System.Net;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
// Create a request for the URL.
WebRequest request = WebRequest.Create(
"http://www.contoso.com/default.html");
// If required by the server, set the credentials.
request.Credentials = CredentialCache.DefaultCredentials;
// Get the response.
WebResponse response = request.GetResponse();
// Display the status.
Console.WriteLine(((HttpWebResponse)response).StatusDescription);
// Get the stream containing content returned by the server.
Stream 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 and the response.
reader.Close();
response.Close();
}
}
}