I am newer than "egg" in .NET and C# and wanted to test whether I am getting HTTP Response (GET) or not. Since working behind the firewall, I am not sure whether the problem lies in Code or Security.
Code which is copied from http://www.csharp-station.com/howto/httpwebfetch.aspx
Code:
using System;
using System.IO;
using System.Net;
using System.Text;
/// <summary>
/// Fetches a Web Page
/// </summary>
class WebFetch
{
static void Main(string[] args)
{
// used to build entire input
StringBuilder sb = new StringBuilder();
// used on each read operation
byte[] buf = new byte[8192];
// prepare the web page we will be asking for
HttpWebRequest request = (HttpWebRequest)
WebRequest.Create("http://www.mayosoftware.com");
// execute the request
HttpWebResponse response = (HttpWebResponse)
request.GetResponse();
// we will read data via the response stream
Stream resStream = response.GetResponseStream();
string tempString = null;
int count = 0;
do
{
// fill the buffer with data
count = resStream.Read(buf, 0, buf.Length);
// make sure we read some data
if (count != 0)
{
// translate from bytes to ASCII text
tempString = Encoding.ASCII.GetString(buf, 0, count);
// continue building the string
sb.Append(tempString);
}
}
while (count > 0); // any more data to read?
// print out page source
Console.WriteLine(sb.ToString());
}
}
Error:
Server Error in '/' Application.
Parser Error Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.
Parser Error Message: 'WebApplication6._Default' is not allowed here because it does not extend class 'System.Web.UI.Page'.
Source Error:
Line 1: <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" Line 2:
CodeBehind="Default.aspx.cs" Inherits="WebApplication6._Default" %> Line 3:
Any hint, about how to solve this problem. Very noob so will appreciate the "baby steps" very much.
I believe your problem is that you're using the wrong project type here. The error message you are seeing is from ASP.NET. The code you're attempting to use is for a Console Application.
The simplest fix is to start a new project, and make sure to choose the correct project type (Console Application).
If you actually want this to be an ASP.NET website, you need to make sure to include a page that derives from System.Web.UI.Page.
Your code appears to be that of a console app, an application that compiles to an .EXE and can be run from the command line.
However, your error message is that of an ASP.NET application; an application designed to run within the process of a web server.
Your question was unclear as to which type of application you're actually trying to build. If it's the former, then all you should need to do is compile your app with Visual Studio or
csc.exe
as an executable (which can be done by right clicking on the project, selecting Properties and setting the Output type to Executable), and run it. If you're having trouble there, I'd suggest just starting over again and creating a new project in Visual Studio, this time selecting "Console App".If you're trying to build a web page, then you have a few problems. First, in your page directive (the
<%@ Page ... %>
thing), you'll need to set theInherits
attribute to the name of your class. For example,WebFetch
. Next, this class needs to derive fromSystem.Web.UI.Page
:If you do this, you should probably override the
Render()
method and write directly to the output stream: