http的GET請求和檢視返回結果
阿新 • • 發佈:2019-01-22
go語言http.Get
package main
import (
"fmt"
"io/ioutil"
"log"
"net/http"
)
func main() {
res, err := http.Get("http://baidu.com")
if err != nil {
log.Fatal(err)
}
robots, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
log.Fatal(err )
}
fmt.Printf("%s\n", res.Status)
fmt.Printf("%s", robots)
}
讀請求
package main
import (
"fmt"
"io"
"log"
"net/http"
)
func main() {
res, err := http.Get("http://baidu.com")
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", res.Status)
buf := make ([]byte,1024)
var ierr error
var len int
for ierr == nil {
len,ierr = res.Body.Read(buf)
fmt.Printf("%s",string(buf[:len]))
}
if ierr != io.EOF{
fmt.Printf("%+v",err)
}
res.Body.Close()
}