Groovy - File handling from http URL

前端 未结 4 1869
旧巷少年郎
旧巷少年郎 2021-01-03 12:32

The files in one of our servers can be accessed via http. So, when we bring up a url similar to the following, we get a list of files/directories in that location:



        
4条回答
  •  天命终不由人
    2021-01-03 13:07

    expadnded version of Grooveek code with https and providing cookie to get to webdavs behind login/password:

    @Grab(group='org.jsoup', module='jsoup', version='1.7.3')
    import org.jsoup.Jsoup
    
    import javax.net.ssl.HostnameVerifier
    import javax.net.ssl.HttpsURLConnection
    import javax.net.ssl.SSLContext
    import javax.net.ssl.TrustManager
    import javax.net.ssl.X509TrustManager
    
    def nullTrustManager = [
    checkClientTrusted: { chain, authType ->  },
    checkServerTrusted: { chain, authType ->  },
    getAcceptedIssuers: { null }
    ]
    
    def nullHostnameVerifier = [
    verify: { hostname, session -> true }
    ]
    
    SSLContext sc = SSLContext.getInstance("SSL")
    sc.init(null, [nullTrustManager as X509TrustManager] as TrustManager[],     null)
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory())
    HttpsURLConnection.setDefaultHostnameVerifier(nullHostnameVerifier as     HostnameVerifier)
    
    def (doc,files,dirs)  = 
        [Jsoup.connect('https://webdav/address').cookie('JSESSIONID','XYZsessionid').get(),[],[]]
    doc.select("[href]").each{href ->
        def filename = href.text()
        def path = href.attr('href')
        path.endsWith("/")?dirs.add(filename):files.add(filename)
        }
    println """DIRECTORIES :
    ${dirs.join('\n')}
    FILES : 
    ${files.join('\n')}
    """
    

提交回复
热议问题