无法取得网页 [英] Unable to Fetch a Webpage

查看:206
本文介绍了无法取得网页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我比.NET和C#中的egg更新,并想测试我是否获得HTTP响应(GET)。由于在防火墙后工作,我不确定问题出在代码还是安全。



http://www.csharp-station.com/howto/httpwebfetch.aspx



Code:

  using System; 
使用System.IO;
使用System.Net;
使用System.Text;


///< summary>
///获取网页
///< / summary>
class WebFetch
{
static void Main(string [] args)
{
//用于构建整个输入
StringBuilder sb = new StringBuilder );

//用于每个读取操作
byte [] buf = new byte [8192];

//准备我们要求的
的网页HttpWebRequest request =(HttpWebRequest)
WebRequest.Create(http://www.mayosoftware.com);

//执行请求
HttpWebResponse response =(HttpWebResponse)
request.GetResponse();

//我们将通过响应流读取数据
Stream resStream = response.GetResponseStream();

string tempString = null;
int count = 0;

do
{
//用数据
count = resStream.Read(buf,0,buf.Length)填充缓冲区;

//确保我们读取一些数据
if(count!= 0)
{
//从字节转换为ASCII文本
tempString = Encoding.ASCII.GetString(buf,0,count);

//继续生成字符串
sb.Append(tempString);
}
}
while(count> 0); //还有更多数据要读取?

//打印页面源
Console.WriteLine(sb.ToString());


$ / code $ / pre
$ b $ p


'/'应用程序中的服务器错误。



解析器错误描述:解析$ b期间发生错误$ b资源需要服务这个请求。请检查以下
特定的分析错误详细信息,并适当修改您的源文件


解析器错误信息:'WebApplication6._Default'在这里不允许
,因为它没有扩展类'System.Web.UI.Page'。
第1行:<%@ Page Title =主页语言=C#
MasterPageFile =〜/ Site.masterAutoEventWireup =true第2行:

CodeBehind =Default.aspx.csInherits =WebApplication6._Default%>
第3行:

任何提示,关于如何解决这个问题。非常小巧,所以非常欣赏宝贝步骤。 解决方案

您的代码看起来像是一个控制台应用程序,应用程序编译为.EXE,并可以从命令行运行。



但是,您的错误消息是ASP.NET应用程序的错误消息;一个设计用于在Web服务器进程中运行的应用程序。



您的问题不清楚您尝试使用哪种类型的应用程序 尝试建立。如果它是前者,那么你所需要做的就是用Visual Studio或 csc.exe 编译你的应用程序作为可执行文件(这可以通过右键单击项目来完成,选择属性并将输出类型设置为可执行),然后运行它。如果您遇到问题,我建议您重新开始并在Visual Studio中创建一个新项目,这次选择控制台应用程序。



如果您试图建立一个网页,那么你有一些问题。首先,在你的页面指令中(<%@ Page ...%> 东西),你需要设置 Inherits 属性为您班级的名称。例如, WebFetch 。接下来,这个类需要从 System.Web.UI.Page

  ///< summary> 
///获取网页
///< / summary>
public class WebFetch:System.Web.UI.Page
{
// ...
}

如果你这样做,你应该重写 Render()方法并直接写入输出流: p>

  ///< summary> 
///获取网页
///< / summary>
public class WebFetch:System.Web.UI.Page
{
保护覆盖无效渲染(HtmlTextWriter作家)
{
//所有代码在这里

writer.Write(sb.ToString());
}
}


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.

解决方案

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 the Inherits attribute to the name of your class. For example, WebFetch. Next, this class needs to derive from System.Web.UI.Page:

/// <summary>
/// Fetches a Web Page
/// </summary>
public class WebFetch : System.Web.UI.Page
{
  //...
}

If you do this, you should probably override the Render() method and write directly to the output stream:

/// <summary>
/// Fetches a Web Page
/// </summary>
public class WebFetch : System.Web.UI.Page
{
   protected override void Render(HtmlTextWriter writer)
   {
      // All your code here

      writer.Write(sb.ToString());
   }
}

这篇关于无法取得网页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆