VB.NET:如何将压缩网页检索为字符串?
我正试图从网站上提取一些文本进行排序 使用如下函数:VB.NET:如何将压缩网页检索为字符串?,vb.net,vb.net-2010,Vb.net,Vb.net 2010,我正试图从网站上提取一些文本进行排序 使用如下函数: Private Function GetContent(ByRef strUrlAddress as String) as String Dim ResultString as String = New System.Net.WebClient().DownloadString(strUrlAddress) Return ResultString End Function 从某些站点检索文本。但是其他站点返回的是压缩
Private Function GetContent(ByRef strUrlAddress as String) as String
Dim ResultString as String = New System.Net.WebClient().DownloadString(strUrlAddress)
Return ResultString
End Function
从某些站点检索文本。但是其他站点返回的是压缩数据,字符串最终包含了大量的数据。如何检索已解压缩的页面,或将数据解压缩为已检索的页面?解决方案来自于在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
添加这一行之后,检索甚至压缩的站点似乎都能正常工作。你能提供可公开访问的示例吗?@dummy对我来说最简单的演示是使用curl获取api.stackexchange.com,然后检索api.stackexchange.com/sites(在文档中总是压缩的JSON)。第二个站点是数据,但如果使用curl--compressed,则可以看到文本。