vb 如何获取网页读取状态_HTTP处理程序以读取网页

vb 如何获取网页读取状态

Generally in ASP.NET we are used to working with .ASPX file most. ASPX file is heart of ASP.NET technology. Basically ASPX file represent HTML kind of representation which used to bind with ASPX.CS file which is largely known as code-behind file. When you run ASPX file, it compiles code-behind and display the results, but, have you ever thought that you could bypass code-behind file?

通常,在ASP.NET中,我们最常使用.ASPX文件。 ASPX文件是ASP.NET技术的核心。 基本上,ASPX文件表示HTML形式的表示形式,用于与ASPX.CS文件绑定,而ASPX.CS文件通常被称为代码隐藏文件。 当您运行ASPX文件时,它会编译代码隐藏文件并显示结果,但是,您是否曾经想过可以绕过代码隐藏文件?

ASXH (HTTP Handler) is really very powerful but under utilized utility. Even if you search GOOGLE, you won’t find many examples. I tried to GOOGLE this topic and found mostly one example of retrieving images with HTTP Handler.  HTTP Handler is not limited to retrieve images only but it is really VERY powerful tool to use.

ASXH(HTTP处理程序)确实非常强大,但使用的工具很少。 即使您搜索GOOGLE,也不会找到很多示例。 我尝试使用GOOGLE这个主题,并且发现了一个使用HTTP处理程序检索图像的示例。 HTTP处理程序不仅限于检索图像,而且它确实是非常强大的工具。

I thought to present different example of HTTP Handler so I prepared one small demo of it which read the URL given in that.

我本想提供一个HTTP处理程序的不同示例,所以我准备了一个小型演示程序来读取其中给出的URL。

NOTE: this is just a very BASIC demo; you can customize the code and can use it for your own purpose. It has lots of scope for improvement but the intention of this article is to show you different usage of HTTP Handler only.

注意:这只是一个非常基本的演示; 您可以自定义代码,并可以将其用于自己的目的。 它有很多改进的余地,但是本文的目的只是向您展示HTTP处理程序的不同用法。

Please follow the below given steps to make one sample example.

请按照以下给出的步骤制作一个示例示例。

1.)    Create new project in VS 2008, steps are: File->New->Project->Visual C#->Asp.Net web application

1.)在VS 2008中创建新项目,步骤为:File-> New-> Project-> Visual C#-> Asp.Net Web应用程序

2.)    Right click on website name in solution explorer and click one Add->New Item->Generic Handler

2.)在解决方案资源管理器中右键单击网站名称,然后单击一个Add-> New Item-> Generic Handler。

3.)    Give the name “Handler.ashx” to the file and click on “Add” button.

3.)给文件起一个名字“ Handler.ashx”,然后点击“添加”按钮。

4.)    Copy and paste the below given code in your Handler.ashx.cs file. (You should already have Default.aspx file in your website but we don’t want to add anything in that, rather we will execute that page only when we will finish code)

4.)将以下给定的代码复制并粘贴到Handler.ashx.cs文件中。 (您的网站中应该已经有Default.aspx文件,但是我们不想在其中添加任何内容,而是仅在完成代码后才执行该页面)

using System;
using System.Web;
using System.Net;
using System.IO;
using System.Text.RegularExpressions;

public class Handler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        const string ROOT = "http://www.SQLHub.com";

        string url = context.Request.QueryString.ToString();

        CookieContainer CC = new CookieContainer();

        HttpWebRequest Req = (HttpWebRequest)WebRequest.Create(ROOT + HttpUtility.UrlDecode(url));
        Req.CookieContainer = CC;

        try
        {
            WebResponse webResponse = Req.GetResponse();
            string sTxt = new System.IO.StreamReader(webResponse.GetResponseStream(),
                System.Text.Encoding.UTF8).ReadToEnd();
            webResponse.Close();

            context.Response.ContentType = webResponse.ContentType;
            context.Response.ContentEncoding = System.Text.Encoding.UTF8;
            context.Response.Write(sTxt);
        }
        catch
        {
            context.Response.Redirect(url);
        }

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

5.)    Go to your web.config file, add following one line under Configuration->System.Web->httpHandler

5.)转到您的web.config文件,在Configuration-> System.Web-下添加以下一行 > httpHandl er

<add verb="*" path="Default.aspx" type="Handler"/>

6.)    That’s it; you are now ready to run your application without even writing single line of code in your default.aspx. Open your Default.aspx page by double clicking on it from solution explorer and run your website by pressing F5 key.

6.)就这样; 您现在可以运行应用程序,而无需在default.aspx中编写任何代码。 在解决方案资源管理器中双击以打开Default.aspx页面,然后按F5键运行您的网站。

Hope you have enjoyed this read. do give me your feedback.

希望您喜欢阅读。 给我您的反馈。

Thanks,

谢谢,

Ritesh Shah

里特斯·沙(Ritesh Shah)

翻译自: https://www.experts-exchange.com/articles/2307/HTTP-Handler-to-read-webpage.html

vb 如何获取网页读取状态

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值