0

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

使用这样的函数:

Private Function GetContent(ByRef strUrlAddress as String) as String

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

End Function

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

4

1 回答 1

2

该解决方案来自在 C# 中为 .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

添加该行之后,检索甚至压缩的站点现在似乎可以正常工作。

于 2013-09-21T04:38:18.197 回答