简体   繁体   English

VB.net下载URL的HTML

[英]VB.net Download HTML of a URL

Im in need of downloading the HTML generated by a server after sending a URL. 我需要在发送URL后下载服务器生成的HTML。 I thought maybe the webclient.downloadfile would work but it seems like it gives up when you don't end the URL with a file extension. 我想也许webclient.download文件可以工作,但是当你没有用文件扩展名结束URL时,它似乎放弃了。

If I do have a webbrowser control set up that has navigated to a site on the same domain;however, that webbrowser has a handler set up for the documentcompleted event that I do not want to fire. 如果我有一个已导航到同一域上的站点的webbrowser控件设置;但是,该webbrowser具有为我不想触发的documentcompleted事件设置的处理程序。

If the best solution is to load the page and use the webbrowser.documentstream to get a stream and write it to a file I have 2 questions: 如果最好的解决方案是加载页面并使用webbrowser.documentstream获取流并将其写入文件我有2个问题:

1) the document.stream returns a system.io.stream and the file object uses system.io.filestream. 1)document.stream返回system.io.stream,文件对象使用system.io.filestream。 Can I easily write a stream to a filestream and if so, how? 我可以轻松地将流写入文件流,如果是,如何?

2) I answered this question for myself when I was typing it out :P 2)我在输入时自己回答了这个问题:P

If this is not the best solution and the webclient can infact download a file after it's generated, how would I go about that?\\ 如果这不是最好的解决方案,并且webclient可以在文件生成后进行实际下载,那么我该如何处理呢?

Edit: If I'm way off base, don't hesitate to let me know :) I'm still quite new to VB 编辑:如果我离开基地,请不要犹豫让我知道:)我还是VB的新手

edit: The below is not working. 编辑:以下不起作用。 My step through revealed that I am in fact passing usable data to the functions, but for some reason the files are still not being created. 我的步骤显示我实际上将可用数据传递给函数,但由于某种原因,文件仍未创建。

Uniencoding declaration: Uniencoding声明:

 Dim uniencoding As New System.Text.UnicodeEncoding()

Code: 码:

 request = WebRequest.Create(tempstring)
    Using response As WebResponse = request.GetResponse()
        Using reader As New StreamReader(response.GetResponseStream())
            Dim html As String = reader.ReadToEnd()
            fstream = New FileStream(surl, FileMode.Create)
            fstream.Write(uniencoding.GetBytes(html), 0, Len(uniencoding.GetBytes(html))) 'Write the HTML to a local file
        End Using
    End Using

If all you want to do is get the HTML (or whatever the response contains), without actually rendering it to the screen, you can do that easily using the WebRequest class. 如果您只想获取HTML(或响应中包含的任何内容),而不实际将其呈现在屏幕上,则可以使用WebRequest类轻松完成。

Dim request As WebRequest = WebRequest.Create("http://www.google.com")
using response As WebResponse = request.GetResponse()
    Using reader As New StreamReader(response.GetResponseStream())
        Dim html As String = reader.ReadToEnd()
        File.WriteAllText("test.html", html)
    End Using
End Using

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM