[VB] WebDAVを使う | Archive Redo Blog

Archive Redo Blog

DBエンジニアのあれこれ備忘録

VB や VBA から WebDAV を使ったファイル転送を行うには Microsoft XML Parser(MSXML) に含まれるXMLHttpRequest を使用すればいいようです。

XMLHttpRequest は元々JavaScript などから HTTP 通信を行うための API として生まれたものですが、WebDAV は HTTP の拡張プロトコルですから、WebDAV でも使えるというわけです。

例えば、WebDAV 共有フォルダのプロパティ(全て)を取得する場合は、以下のようなコーディングとなります。

Private Sub main()
   
   Dim sURL         As String
   Dim sUserID      As String
   Dim sPassword    As String
   Dim req          As MSXML.XMLHTTPRequest
   Dim sReq         As String
   
   sURL = "http://localhost:8080/webdav/"
   sUserID = "tomcat"
   sPassword = "tomcat"

   Set req = CreateObject("MICROSOFT.XMLHTTP")

   req.Open "PROPFIND", sURL, False, sUserID, sPassword

   req.setRequestHeader "Content-Type", "text/xml"
   sReq = "<?xml version='1.0'?>"
   sReq = sReq & "<propfind xmlns='DAV:'>"
   sReq = sReq & "<allprop/>"
   sReq = sReq & "</propfind>"

   req.send sReq

   Debug.Print req.responseText

End Sub

※"Microsoft XML"への参照設定が必要です。

XMLHTTPRequest オブジェクトを作成し、接続をオープンし、リクエストを送信し、結果を受信する...と処理の流れは非常にシンプルです。

上記のコードを実行した結果は以下のように XML で返されます。

<?xml version="1.0" encoding="utf-8" ?>
<multistatus xmlns="DAV:">
  <response>
    <href>/webdav/</href>
    <propstat>
      <prop>
        <creationdate>2007-06-01T05:11:22Z</creationdate>
        <displayname><![CDATA[]]></displayname>
        <resourcetype><collection/></resourcetype>
        <source></source>
        <supportedlock>
          <lockentry>
            <lockscope><exclusive/></lockscope>
            <locktype><write/></locktype>
          </lockentry>
          <lockentry>
            <lockscope><shared/></lockscope>
            <locktype><write/></locktype>
          </lockentry>
        </supportedlock>
      </prop>
      <status>HTTP/1.1 200 OK</status>
    </propstat>
  </response>
  <response>
    <href>/webdav/tomcat.gif</href>
    <propstat>
    <prop>
      <creationdate>2005-09-23T13:42:06Z</creationdate>
      <displayname><![CDATA[tomcat.gif]]></displayname>
      <getlastmodified>Fri, 23 Sep 2005 13:42:06 GMT</getlastmodified>
      <getcontentlength>1934</getcontentlength>
      <getcontenttype>image/gif</getcontenttype>
      <getetag>W/"1934-1127482926023"</getetag>
      <resourcetype/>
      <source></source>
      <supportedlock>
        <lockentry>
          <lockscope><exclusive/></lockscope>
          <locktype><write/></locktype>
        </lockentry>
        <lockentry>
          <lockscope><shared/></lockscope>
          <locktype><write/></locktype>
        </lockentry>
      </supportedlock>
    </prop>
    <status>HTTP/1.1 200 OK</status>
    </propstat>
  </response>
  <response>
    <href>/webdav/tomcat-power.gif</href>
    <propstat>
      <prop>
        <creationdate>2005-09-23T13:42:06Z</creationdate>
        <displayname><![CDATA[tomcat-power.gif]]></displayname>
        <getlastmodified>Fri, 23 Sep 2005 13:42:06 GMT</getlastmodified>
        <getcontentlength>2324</getcontentlength>
        <getcontenttype>image/gif</getcontenttype>
        <getetag>W/"2324-1127482926934"</getetag>
        <resourcetype/>
        <source></source>
        <supportedlock>
          <lockentry>
            <lockscope><exclusive/></lockscope>
            <locktype><write/></locktype>
          </lockentry>
          <lockentry>
            <lockscope><shared/></lockscope>
            <locktype><write/></locktype>
          </lockentry>
        </supportedlock>
      </prop>
      <status>HTTP/1.1 200 OK</status>
    </propstat>
  </response>
  <response>
    <href>/webdav/index.html</href>
    <propstat>
      <prop>
        <creationdate>2005-09-23T13:42:04Z</creationdate>
        <displayname><![CDATA[index.html]]></displayname>
        <getlastmodified>Fri, 23 Sep 2005 13:42:04 GMT</getlastmodified>
        <getcontentlength>3055</getcontentlength>
        <getcontenttype>text/html</getcontenttype>
        <getetag>W/"3055-1127482924741"</getetag>
        <resourcetype/>
        <source></source>
        <supportedlock>
          <lockentry>
            <lockscope><exclusive/></lockscope>
            <locktype><write/></locktype>
          </lockentry>
          <lockentry>
            <lockscope><shared/></lockscope>
            <locktype><write/></locktype>
          </lockentry>
        </supportedlock>
      </prop>
      <status>HTTP/1.1 200 OK</status>
    </propstat>
  </response>
</multistatus>


上記の例は非常に単純なものですが、XMLHTTPRequest のメソッド、リクエストヘッダ、XML の内容を変えることで、様々な処理を行うことができます。


ただし、この XMLHttpRequest の仕様に関しては日本語で体系的にまとめられた資料がなかなか見当たりません。

また、データの受け渡しに XML を使いますので、これを解析するのもまた面倒です。

単純な処理にとどめるならいいですが、複雑なことをしようと思うと結構大変かもしれません。


XMLHttpRequest Object (MSDN)

The XMLHttpRequest Object(W3C Working Draft)