建立server
访问别的server
##get
package main
import (
"net/http"
"io/ioutil"
"fmt"
)
func main(){
resp, err := http.Get("http://www.baidu.com")
if err != nil{
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
##post
package main
import (
"net/http"
"io/ioutil"
"fmt"
"strings"
)
func main(){
resp, err := http.Post("http://baidu.com", "application/x-www-from-urlencoded", strings.NewReader("id=1"))
if err != nil{
panic(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}