0

我正在尝试运行以下内容:

Dim a As Boolean = False
        Try
            Dim myHTTPWebRequest As HttpWebRequest = CType(WebRequest.Create(txtToShorten.Text), HttpWebRequest)
            Dim myHTTPwebResponse As HttpWebResponse = CType(myHTTPWebRequest.GetResponse(), HttpWebResponse)
            If myHTTPwebResponse.StatusCode = HttpStatusCode.OK Then
                a = True
            End If
        Catch e As WebException
        Catch e As Exception
        End Try
        Return a

当我执行正确的 url 时,它可以正常工作并返回 true,但是如果输入的 url 无效,则它不会返回值。

请问有什么想法吗?

4

1 回答 1

0

实际上,这对我来说很好:

Imports System.Net

公开课形式1

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim x As Boolean = bb()
    x = x

End Sub

Private Function bb() As Boolean

    Dim a As Boolean = False
    Try
        Dim myHTTPWebRequest As HttpWebRequest = CType(WebRequest.Create("http:\\www.fokikkkkkrd.com"), HttpWebRequest)
        Dim myHTTPwebResponse As HttpWebResponse = CType(myHTTPWebRequest.GetResponse(), HttpWebResponse)
        If myHTTPwebResponse.StatusCode = HttpStatusCode.OK Then
            a = True
        End If
    Catch ex As WebException
    Catch ex As Exception
    End Try
    Return a

End Function

结束类

你把这段代码放在一个函数里了吗?

于 2013-03-29T15:12:13.513 回答