0

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:

enter image description here

enter image description here

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();
        }
    }
}
11
  • Don't think you have searched too much, if you read any example you will realize that what you want is not a WebRequest but an HttpWebRequest... Commented Jun 7, 2017 at 22:30
  • The code in the image, is totoally from Microsoft Document. It shows the error. If I should use HttpWebRequest, why the code use WebRequest ? Commented Jun 7, 2017 at 22:38
  • And, I have to say, I just start with C# less about 2 weeks. So, maybe I did very slow. Commented Jun 7, 2017 at 22:39
  • You need to cast it as an HttpWebResponse, WebRequest.Create will return an HttpWebResponse (which inherits WebResponse), just cast it and you will have access to the functions you need. Also, don't post images, add the code to your question. Commented Jun 7, 2017 at 22:41
  • I updated the hyperlinks. It is best that you cut and past code to the body of the question Commented Jun 7, 2017 at 22:55

1 Answer 1

1

Steps below made mine successful

  1. New a solution - a Visual C# Console Application in Visual Studio. Name your project something as "ConsoleApp1".
  2. Copy and paste code within main function from the web, to newly generated Main function.
  3. Add below using statements
using System.Net;
using System.IO;
  1. Run it by press "F5". It's successful. But the window closed as soon as it completes.
  2. For you to see the output result, put a break point at "}" statement to avoid the window to close. Alternatively, you can add below as last C# statement. Then you need to click any key to close the console window.

    Console.ReadKey();

The output, I got:

OK
<html><head><title>Microsoft Corporation</title><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"></meta><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta><meta name="SearchTitle" content="Microsoft.com" scheme=""></meta><meta name="Description" content="Get product information, support, and news from Microsoft." scheme=""></meta><meta name="Title" content="Microsoft.com Home Page" scheme=""></meta><meta name="Keywords" content="Microsoft, product, support, help, training, Office, Windows, software, download, trial, preview, demo,  business, security, update, free, computer, PC, server, search, download, install, news" scheme=""></meta><meta name="SearchDescription" content="Microsoft.com Homepage" scheme=""></meta></head><body><p>Your current User-Agent string appears to be from an automated process, if this is incorrect, please click this link:<a href="http://www.microsoft.com/en/us/default.aspx?redir=true">United States English Microsoft Homepage</a></p></body></html>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks, Herbert. I was using Macbook Pro by the parallel virtual machine and the VS version is Enterprise 2017, .net framework is the same as yours. But after I shifted to a windows laptop, and the code is running perfect. Maybe the problem is the virtual machine ? ... It's very strange. Thanks for your help ! You are so nice. I guess you may come from China and live in USA ? I am also from China and right now live in Canada :)
@AaronGuo In that case, there is something wrong with your virtual machine. Please select this as your answer to your question. I need points now since our company moved to another domain, I lost all my points with my email address. Thank you. Yes, I'm in USA.
haha, I already marked. Thanks. Please help me in the future. I am a front-end developer for some years. Right now I want to expand my knowledge, but beginning is always not easy. I will keep going !
It seems that I was too optimistic. When I use Visual Studio 2017, even I build it on Windows Laptop, the error still shown. So, I think there is a high chance that the problem is Visual Studio 2017...
I'm using VS2015 and Enterprise VS2017RC. On VS2017RC, this program ran successfully just now. My VS is Microsoft Visual Studio Enterprise 2017RC Visual Studio/15.0.0-RC.3+26127.3 .Net Framework is version 4.7.02046

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.