2011-08-17 51 views
0

它總是捕捉異常,並輸出到檢索XML文件時「無法讀取數據$ DID:$別名」當我運行閱讀():HttpResponseException嘗試使用Groovy腳本

http = new HTTPBuilder('https://somewebsite.com') 

def read(http, path, dId, alias, portalFile, outputFileName) { 

    try { 
    println "Reading : path:$path, file:$portalFile" 

     http.get(path: path, 
      contentType: TEXT, 
      query: [id:dId, instance:alias, format:'xml', file:portalFile]) {resp, reader -> 

      println "response status: ${resp.statusLine}" 
     println 'Headers: -----------' 
     resp.headers.each { h ->  
      println " ${h.name} : ${h.value}" 
     } 

     new File(outputFileName).withWriter{out -> out << reader} 
     } 
    } catch (HttpResponseException h) { 
    println "Unable to read data for $dId:$alias" 
    } 
} 

如果我去該網站使用我的互聯網瀏覽器,並點擊我需要的XML文件,它的工作原理。有什麼方法可以輸出它連接的URL嗎?

回答

0

要查看事情發生錯誤的最佳方法是爲http生成器啓用線程調試 - 這會向您顯示正在發生的確切請求和響應。從這裏你可以看到正在提取哪個URL,具有什麼參數和標題,以及服務器返回的內容。

啓用調試記錄在這裏 - http://groovy.codehaus.org/modules/http-builder/doc/index.html#Logging_and_Debugging

例如以下內容添加到您的log4j.properties

log4j.logger.org.apache.http.headers=DEBUG 
log4j.logger.org.apache.http.wire=DEBUG 

或者,更快捷,更髒,把事情處理好,在你的Groovy腳本的頂部(因人而異)

System.setProperty('org.apache.commons.logging.Log', 'org.apache.commons.logging.impl.SimpleLog') 
System.setProperty('org.apache.commons.logging.simplelog.log.org.apache.http.wire', 'DEBUG')