【问题标题】:Check if folder on web exists or not检查网络上的文件夹是否存在
【发布时间】:2014-02-09 08:07:30
【问题描述】:

我正在创建桌面应用程序,当我在 TextBox1Button1.Click 事件中写入用户名时,它应该检查网络上的文件夹是否存在。
到目前为止,我已经尝试过:

 username = Me.TextBox1.Text
 password = Me.TextBox2.Text

        Dim dir As Boolean = IO.Directory.Exists("http://www.mywebsite.com/" + username)
        If dir = true Then
            Dim response As String = web.DownloadString("http://www.mywebsite.com/" + username + "/Password.txt")
            If response.Contains(password) Then
                MsgBox("You've logged in succesfully", MsgBoxStyle.Information)
                Exit Sub
            Else
                MsgBox("Password is incorect!")
            End If
        Else
            MsgBox("Username is wrong, try again!")
        End If

第一个问题是我的布尔值给出了 FALSE 作为答案(目录确实存在并且所有权限都被授予查看文件夹)。我试图通过设置dir = false 来解决这个问题,然后我进入第一个 IF(但这不是我想要的,因为它应该是 TRUE,而不是 FALSE)
我们来到第二个问题,在这一行:Dim response As String=web.DownloadString("http://www.mywebsite.com/" + username + "/Password.txt") 我收到此错误消息:The remote server returned an error: (404) Not Found.
有没有比这方面经验更丰富的可以帮帮我?

【问题讨论】:

    标签: vb.net visual-studio-2012


    【解决方案1】:

    IO.Directory.Exists 在这种情况下不起作用。该方法仅适用于检查某处(本地或网络)磁盘上的文件夹;您不能使用它来检查 HTTP 上的资源是否存在。 (即 URI)

      1234563 404 Not Found,这意味着“就您而言,此资源不存在”。 **
    • 所以你应该try/catch操作,你需要捕获WebException类型的异常,将其Response成员转换为HttpWebException,并检查StatusCode属性。 一个很好的例子(尽管在 C# 中)是here

    ** 我说“就您而言”,因为据您所知,该资源很可能存在于服务器上,但它决定对您隐藏它,因为您无权访问它等,并且该站点的开发人员决定在这种情况下返回 404 而不是 401 Unauthorized。关键是从您的角度来看,该资源不可用。

    更新:

    这是我链接到的答案中的代码,通过this online tool 翻译,因为我的 VB 足够狡猾:)。这段代码在 LinqPad 中运行得很好,并产生输出“testlozinka

    Sub Main
    Try
        Dim myString As String
        Using wc As New WebClient()
            myString = wc.DownloadString("http://dota2world.org/HDS/Klijenti/TestKlijent/password.txt")
            Console.WriteLine(myString)
        End Using
    Catch ex As WebException
     Console.WriteLine(ex.ToString())
        If ex.Status = WebExceptionStatus.ProtocolError AndAlso ex.Response IsNot Nothing Then
            Dim resp = DirectCast(ex.Response, HttpWebResponse)
            If resp.StatusCode = HttpStatusCode.NotFound Then
                ' HTTP 404
                'the page was not found, continue with next in the for loop
                Console.WriteLine("Page not found")
            End If
        End If
        'throw any other exception - this should not occur
        Throw
    End Try
    End Sub
    

    希望对您有所帮助。

    【讨论】:

    • 您能否将您给我的链接中的答案“翻译”到 Vb.net 中?我不太擅长 C#。顺便说一句,谢谢你的回答,我发现 IO.Directory.Exists 在这种情况下甚至没有意义。
    • 我已经改变了一些东西,这样我已经完成了这应该可以工作,但我仍然收到有关 404 错误的消息。我是我上传和打开文件夹的主机和域的所有者,我将权限更改为 7 7 7,这意味着这应该可以正常工作。
    • 查看已翻译代码的更新答案。关于 404 - 如果您在浏览器中打开此 URL,您可以访问该文件吗?
    • 我试过你的方法仍然得到同样的错误信息。我可以打开我的网址,您可以查看HERE,您可以看到我可以从该链接中读取字符串
    • 嗯。请参阅更新的答案。我在 LinqPad 中运行了代码,效果很好。
    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 2011-03-06
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-05
    • 1970-01-01
    • 2012-04-22
    相关资源
    最近更新 更多