如何在申请中提出申请

时间:2016-08-18 23:07:03

标签: google-app-engine go

如何在GAE应用程序中创建GET HTTP请求?我不想将它作为处理函数,我只需要一个我需要的URL来获取响应体。

2 个答案:

答案 0 :(得分:1)

使用urlfetch包。

ctx := appengine.NewContext(r)   // r is the *http.Request arg to the handler
client := urlfetch.Client(ctx)  
resp, err := client.Get("http://example.com")
if err != nil {
    // handle the error 
}
body := resp.Body // body is an io.Reader containing the response body

Here's a complete example

答案 1 :(得分:0)

如果这不起作用,我没有使用它如此道歉。根据{{​​3}},您可能希望使用urlfetch来获得*http.Client之类的内容(N.B. context包是刚刚发布的Go 1.7中的标准内容):

import (
    "context" // Go 1.7
    // "golang.org/x/net/context" // Go < 1.7
    "google.golang.org/appengine/urlfetch"
)

client := urlfetch.Client(context.Background())
resp, err := client.Get("http://example.com/")