超慢的HttpWebRequest

时间:2011-03-10 03:21:42

标签: vb.net httpwebrequest html-agility-pack

我正在为我正在做的项目制作一个网站刮刀。我已经完成了一切工作,但是,加载实际页面需要F-O-R-E-V-E-R。你可以在这里看到它正在加载的页面: MCServerList.Net

以下是我正在使用的代码:

private CONST REQUESTURL as string = "http://www.MCServerList.net/?page="
private chunkId as int32 = 1

Dim req As HttpWebRequest = WebRequest.Create(REQUESTURL & chunkId)
Dim res As HttpWebResponse = req.GetResponse()
Dim Stream As Stream = res.GetResponseStream()

然后我使用“Stream”并通过在线免费发现的HTMLAgilityPack加载它。它会快速加载页面,但初始请求通常需要大约20-30秒。

任何帮助将不胜感激!

1 个答案:

答案 0 :(得分:1)

我刚刚运行了以下代码并忽略了GetResponse()的第一次初始编译我平均约3.3秒和Load()的0.2秒。你在快速连接吗?你确定这是瓶颈所在吗?

Option Explicit On
Option Strict On

Imports System.Net

Public Class Form1

    Private Const REQUESTURL As String = "http://www.MCServerList.net/?page="
    Private chunkId As Int32 = 1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim ST As New System.Diagnostics.Stopwatch()
        ST.Start()
        Dim req = WebRequest.Create(REQUESTURL & chunkId)

        Dim res = req.GetResponse()
        Trace.WriteLine(String.Format("GetResponse() : {0}", ST.Elapsed))
        Using Stream As System.IO.Stream = res.GetResponseStream()
            Dim X As New HtmlAgilityPack.HtmlDocument()
            X.Load(Stream)
        End Using
        Trace.WriteLine(String.Format("Load() : {0}", ST.Elapsed))
        ST.Stop()
    End Sub
End Class