VB.net 下载 URL 的 HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19181470/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:21:23  来源:igfitidea点击:

VB.net Download HTML of a URL

htmlvb.netinternet-explorerdownloadfilestream

提问by gNerb

Im in need of downloading the HTML generated by a server after sending a URL. 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.

我需要在发送 URL 后下载服务器生成的 HTML。我想也许 webclient.downloadfile 可以工作,但当你不以文件扩展名结束 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 获取流并将其写入文件,我有两个问题:

1) the document.stream returns a system.io.stream and the file object uses system.io.filestream. Can I easily write a stream to a filestream and if so, how?

1) document.stream 返回一个 system.io.stream,文件对象使用 system.io.filestream。我可以轻松地将流写入文件流吗?如果可以,怎么写?

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?\

如果这不是最佳解决方案,并且 web 客户端实际上可以在生成文件后下载文件,我将如何处理?\

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:

统一编码声明:

 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

回答by Steven Doggart

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 WebRequestclass.

如果您只想获取 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