vb.NET WebRequest读取aspx页面到字符串,访问被拒绝?

发布于 2024-09-28 11:47:30 字数 740 浏览 8 评论 0原文

我正在尝试在 VS2008 中创建一个可执行文件,它将使用 vb.NET 函数将网页源代码读取到字符串变量中。问题是该页面不是 *.html,而是 *.aspx。

我需要一种方法来执行 aspx 并将显示的 html 转换为字符串。

我想阅读的页面是此类型的任何页面:http://www.realtor。 ca/PropertyDetails.aspx?PropertyID=9620716

我尝试了以下代码,该代码适用于 html 页面,但当我传入上面的 aspx 页面时,会生成页面标题“访问被拒绝”的错误源代码。

    Dim myReq As WebRequest = WebRequest.Create(url)

    Dim myWebResponse As WebResponse = myReq.GetResponse()

    Dim dataStream As Stream = myWebResponse.GetResponseStream()

    Dim reader As New StreamReader(dataStream, System.Text.Encoding.UTF8)

    Dim responseFromServer As String = reader.ReadToEnd()

有什么建议或想法吗?

I'm trying to make an executable in VS2008 that will read a webpage source code using a vb.NET function into a string variable. The problem is that the page is not *.html but rather *.aspx.

I need a way to execute the aspx and get the displayed html into a string.

The page I want to read is any page of this type: http://www.realtor.ca/PropertyDetails.aspx?PropertyID=9620716

I have tried the following code, which works properly for html pages, but generates the wrong source code with "access denied" for the page title when I pass in the above aspx page.

    Dim myReq As WebRequest = WebRequest.Create(url)

    Dim myWebResponse As WebResponse = myReq.GetResponse()

    Dim dataStream As Stream = myWebResponse.GetResponseStream()

    Dim reader As New StreamReader(dataStream, System.Text.Encoding.UTF8)

    Dim responseFromServer As String = reader.ReadToEnd()

Any suggestions or ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

私藏温柔 2024-10-05 11:47:30

从命令行运行 wget 时我得到了同样的结果:

wget http://www.realtor.ca/PropertyDetails.aspx?PropertyID=9620716

我猜服务器依赖于在传递响应之前在浏览器中设置的某些内容,例如 cookie。您可能想尝试使用 WebBrowser 控件(你不必让它可见)通过以下方式(这有效):

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf DocumentCompletedHandler)
        WebBrowser1.Navigate("http://www.realtor.ca/PropertyDetails.aspx?PropertyID=9620716")
    End Sub

    Private Sub DocumentCompletedHandler(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
        Console.WriteLine(WebBrowser1.DocumentText)
    End Sub
End Class

I get the same thing while running wget from the command line:

wget http://www.realtor.ca/PropertyDetails.aspx?PropertyID=9620716

I guess the server is relying on that something is set in the browser before the response is delivered, e.g. a cookie. You might want to try using a WebBrowser control (you don't have to have it visible) in the following way (this works):

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler WebBrowser1.DocumentCompleted, New WebBrowserDocumentCompletedEventHandler(AddressOf DocumentCompletedHandler)
        WebBrowser1.Navigate("http://www.realtor.ca/PropertyDetails.aspx?PropertyID=9620716")
    End Sub

    Private Sub DocumentCompletedHandler(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)
        Console.WriteLine(WebBrowser1.DocumentText)
    End Sub
End Class
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文