VB.NET:如何将压缩网页检索为字符串? [英] VB.NET: How to retrieve a compressed web page to a string?

查看:17
本文介绍了VB.NET:如何将压缩网页检索为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从网站中提取一些文本进行排序.

I'm trying to pull some text to sort from a website.

使用这样的函数:

Private Function GetContent(ByRef strUrlAddress as String) as String

     Dim ResultString as String = New System.Net.WebClient().DownloadString(strUrlAddress)
     Return ResultString

End Function

从一些网站检索文本很好.但是其他站点正在返回压缩数据,并且字符串最终包含数据块.如何检索解压后的页面,或在检索到数据时解压数据?

Retrieves the text from some sites fine. But other sites are returning compressed data, and the string ends up containing blobs of data. How do I retrieve the decompressed page, or decompress the data as its retrieved?

推荐答案

该解决方案来自在 C# 中针对 .NET 的示例的搜索.

The solution came from searches that had examples in C# for .NET.

  Dim strSitesReply As String = ""

Try
     Dim Request As HttpWebRequest = WebRequest.Create(strUrlAddress)
     ' Here is the important part, using .AutomaticDecompression
     Request.AutomaticDecompression = DecompressionMethods.Deflate
     Dim response As HttpWebResponse = Request.GetResponse()
     Using Reader As StreamReader = New StreamReader(Response.GetResponseStream())
          strSitesReply = Reader.ReadToEnd()
     End Using
Catch ex As Exception
     MsgBox("Error: " + ex.Message)
end Try

添加该行后,检索压缩的网站现在似乎也可以正常工作了.

After adding that line, retrieving even compressed sites seemed to now work properly.

这篇关于VB.NET:如何将压缩网页检索为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆