Reputation: 1
I would like to list files on remote sever (HTTP). Please, advice me on possible options for a console application.
Upvotes: 0
Views: 8371
Reputation: 379
Vbscript list webdav folder (only files, non-recursievly)
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
Upvotes: 0
Reputation: 5614
If your http serer use webdav it is simple to do. Here some example code: Beware that you have to include a reference in your project to MSXML2 Com object
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
}
Upvotes: 0
Reputation: 499382
You can use the WebClient
class to call your server, read the file/directory lists and navigate through the directories recursively.
Here is the basic use of 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
Another option, which may be more suitable, it to use the HTML Agility Pack - you can use the WebHtml
object to retrieve HTML directly from the web and then query it using XPath
syntax.
Upvotes: 1