在浏览器上查看数据

时间:2012-07-06 02:34:25

标签: java url grails urlconnection

我有一个域名,包含URL,lastModified等字段。现在我想查看从此代码到列表页面的信息。我该怎么办

def url = new URL("http://www.google.com")
HttpURLConnection connection = (HttpURLConnection)url.openConnection()
//  long contentLength = Long.parseLong(connection.getHeaderField("Content-Length"));
connection.setRequestMethod("GET")
connection.connect()
connection.getURL()
println("Date: " + new Date(connection.getDate()))
println("LastModified Date:" + new Date(connection.getLastModified()))
if (connection.responseCode == 200 || connection.responseCode == 201){
    def returnMessage = connection.content

    //print out the full response
    println "${returnMessage}";
    System.out.println(connection.getURL());
} else {
    println "Error Connecting to " + url
    println "Couldn't connect to url"
}

1 个答案:

答案 0 :(得分:0)

您可以在控制器中使用render方法

render returnMessage

(见http://grails.org/doc/2.0.x/ref/Controllers/render.html

或将其在模型中传递给gsp页面,然后从gsp打印,

return [returnMsg: returnMessage]

....(in .gsp)
${returnMsg}

(见http://grails.org/doc/2.0.x/guide/theWebLayer.html#GSPBasics