16,553
社区成员
发帖
与我相关
我的任务
分享
''' <summary>
''' 下载文档
''' </summary>
''' <param name="uri">网址</param>
''' <param name="encoder">Encoding编码</param>
''' <returns>下载的文档,失败为nothing</returns>
''' <remarks></remarks>
Protected Friend Shared Function DownloadString(uri As String, Optional ByVal encoder As Encoding = Nothing) As String
Dim value As String = Nothing
If encoder Is Nothing Then
encoder = Encoding.GetEncoding("utf-8")
End If
Dim h As WebClient = Nothing
Try
h = New WebClient()
With h
.Encoding = encoder
.Credentials = CredentialCache.DefaultCredentials '获取或设置用于对向 Internet 资源的请求进行身份验证的网络凭据。
.Headers.Add("Accept", "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*")
.Headers.Add("Accept-Language", "zh-CN")
.Headers.Add("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1)")
value = .DownloadString(uri)
End With
Catch ex As ArgumentOutOfRangeException
value = String.Empty
Catch ex As ArgumentException
value = String.Empty
Catch ex As NotSupportedException
value = String.Empty
Catch ex As WebException
value = String.Empty
Finally
If h IsNot Nothing Then
h.Dispose()
h = Nothing
End If
If value Is Nothing Then
value = String.Empty
End If
End Try
Return value
End Function