列出远程Web服务器(http)上的文件和文件夹

时间:2010-09-07 19:39:43

标签: .net

我想在远程服务器(HTTP)上列出文件。请告诉我有关控制台应用程序的可能选项。

3 个答案:

答案 0 :(得分:1)

您可以使用WebClient类来调用服务器,读取文件/目录列表并以递归方式浏览目录。

以下是WebClient的基本用法:

WebClient client = new WebClient ();
Stream data = client.OpenRead ("http://example.com");
StreamReader reader = new StreamReader(data);
string s = reader.ReadToEnd ();

// At this point, the variable s contains the returned webpage

另一个选项,可能更适合使用HTML Agility Pack - 您可以使用WebHtml对象直接从Web检索HTML,然后使用XPath语法查询它

答案 1 :(得分:0)

如果你的http serer使用webdav,那么这很简单。这里有一些示例代码: 请注意,您必须在项目中包含对MSXML2 Com对象的引用

public static void PrintDirectoryContents(string url, bool deep) {
    var xmlHttp_ = new XMLHTTP();

    // Build the query.
    string requestString = 
        "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
        "<a:propfind xmlns:a=\"DAV:\">" +
        "<a:prop>" +
        "<a:displayname/>" +
        "<a:iscollection/>" +
        "<a:getlastmodified/>" +
        "</a:prop>" +
            "</a:propfind>";

        // Open a connection to the server.
        xmlHttp_.open("PROPFIND", url, false, "youruser", "yourpassword");

        // Send the request.
        xmlHttp_.setRequestHeader("PROPFIND", requestString);
        xmlHttp_.send(null);

        // Get the response.
        string folderList = xmlHttp_.responseText;

        // Parse the folder list.
     XmlDocument XmlDoc = new XmlDocument();
        var xml = folderList;
        XmlDoc.LoadXml(xml);

        //HERE you can read the xmldocument for the 
        //properties of the files you need  
}

答案 2 :(得分:0)

Vbscript列表webdav文件夹(仅文件,非递归)

    Const UploadUser = "loginForWebDav" 'логин для WEBDAV
    Const UploadPass = "passwordForWebDav" 'пароль для WEBDAV
    strURL = "https://login.webdav.hidrive.strato.com/users/login/"
    iterate2ndArray(webDavListOnlyFiles(strURL)) 'return 2D-array 1st array is index, second file name, Date 
    function webDavListOnlyFiles(strURL) 'with trailing slash 'return obj or array?
        Set XMLreq = createobject("MSXML2.XMLHTTP.3.0")
        sSourceURL = backslash2slash(strURL)
        XMLreq.open "PROPFIND", sSourceURL, False, "UploadUser", "UploadPass"   
        XMLreq.setRequestHeader "Content-Type", "text/xml"
        XMLreq.setRequestHeader "Depth", 1 'цифру указывать в кавычказ или нет? - пофиг
        'XMLreq.setRequestHeader "Translate", "f"
        'XMLreq.setRequestHeader "Brief", "t" 'The default setting is "f".
        'XMLreq.send "<?xml version='1.0'?><d:propfind xmlns:d='DAV:'><d:allprop/></d:propfind>"
        XMLreq.send "<?xml version='1.0'?><d:propfind xmlns:d='DAV:'><d:prop><d:resourcetype><d:collection/></d:resourcetype><d:creationdate/></d:prop></d:propfind>"
        'WriteTextFilesStandalone XMLreq.responseText, "C:\shkur\tmpCopy\xml.xml"
        'MsgBox XMLreq.responseXML.getElementsByTagName("D:status").nextNode.Text 'HTTP/1.1 200 OK 'ничего не возвращает если ответ 404
        Set objNodeList1 = XMLreq.responseXML.getElementsByTagName("D:href")
        Set objNodeList2 = XMLreq.responseXML.getElementsByTagName("lp1:creationdate")
        dim arr1st()
        'dim arr2nd() ' несоответствие типа
        ''Set arr1st = CreateObject("Scripting.Dictionary")
        x=0 
        For i = 0 TO (objNodeList1.length -1)
            ''Set arr2nd = CreateObject("Scripting.Dictionary")
            Set objNode1 = objNodeList1.nextNode
            set objNode2 = objNodeList2.nextNode
            If (Right(objNode1.text,1)) <> "/" Then 'trailing slash = folder
            flnm = (mid(objNode1.text,(InStrRev(objNode1.text,"/"))+1))
            creationdate = CDate(Replace(Replace(objNode2.text,"T"," "),"Z"," "))
                'msg = msg & x & ". " & flnm & " "& objNode2.text  &" "&  Vbcrlf
                ''arr2nd.Add "flnm", flnm
                ''arr2nd.Add "creationdate", objNode2.text
                arr2nd = array(flnm, creationdate)
                ReDim Preserve arr1st(x)
                arr1st(x)=arr2nd
                x=x+1
                ''arr1st.Add x, arr2nd 
            End If
        Set arr2nd = Nothing
        Next
        'MsgBox msg
        Set XMLreq = Nothing
        webDavListOnlyFiles = arr1st
        'iterate2ndArray(arr1st)
        'msgbox isarray(arr1st)
        'msgbox isarray(arr1st(0))
        'Set arr1st = Nothing 'несоответствие типа...
    End Function

    function backslash2slash(strUrl)
     'поменять бекслеши на слеши и добавить слеш вконце
     'msgbox backslash2slash("https://www.w3school///s.com/\\\\\vbscript/func_instr.asp")
        leftSide = (Left(strUrl,(InStr(strUrl,"://"))+2))
        rightSide = (Right(strUrl,(Len(strUrl)-InStr(strUrl,"://")-2)))
        rightSide = Replace(Replace(Replace(Replace(rightSide,"\","/"),"///","/"),"//","/"),"//","/")
        concat = leftSide&rightSide
        If (Right(concat,1)) <> "/" Then
            backslash2slash = concat & "/"
        Else 
            backslash2slash = concat
        End If
    End function

    function iterate2ndArray(a)
        if isArray(a) = false then 
            msgbox "это не массив" 
        else
        msg = "begin:"&vbcrlf
        for each x in a
            'msg = msg & "1st array:"& x
            for each xx in x
                msg = msg & "   " & xx
                'msgbox xx
            next
            msg = msg & vbcrlf
        next
        msgbox msg
        end if
    End Function