【问题标题】:Handling non-url errors处理非 url 错误
【发布时间】:2013-03-28 22:58:03
【问题描述】:

我正在尝试运行以下命令:

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 无效,则它不会返回值。

有什么想法吗?

【问题讨论】:

  • 嗨,马克,很抱歉,但我不是 100% 回答这个问题 - 关于如何检测 txtToShorten.Text 中是否提供了无效 URL 的问题?

标签: vb.net httprequest httpresponse


【解决方案1】:

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

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

结束类

你是把这段代码放在函数里面了吗??

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多