System.Net.WebException'发生在System.dll中,但未在用户代码中处理

时间:2017-08-04 06:08:25

标签: asp.net vb.net

Try
    Dim url = "something"
    Dim request = WebRequest.Create(url)
    request.Credentials = CredentialCache.DefaultCredentials
    Dim httpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
    Dim dataStream = httpWebResponse.GetResponseStream()
    If dataStream Is Nothing Then
        Return ""
    End If
    Dim reader = New StreamReader(dataStream)
    Return reader.ReadToEnd()
Catch ex As WebException
    Return ""
End Try

上面的代码在LocalHost中工作,但是在服务器中它不工作并抛出异常。

  

System.Net.WebException'发生在System.dll中但未处理   在用户代码中

1 个答案:

答案 0 :(得分:0)

你在Catch之后重新抛出异常。 删除Throw

 Try
    Dim url = "something"
    Dim request = WebRequest.Create(url)
    request.Credentials = CredentialCache.DefaultCredentials
    Dim httpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
    Dim dataStream = httpWebResponse.GetResponseStream()
    If dataStream Is Nothing Then
        Return ""
    End If
    Dim reader = New StreamReader(dataStream)
    Return reader.ReadToEnd()
Catch ex As WebException
    Return ""
End Try