好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

go的入门之路 http Get Post请求

package main
import (
    "net/http"
    "fmt"
    "io/ioutil"
)
func main()  {
    get()
}
func get(){
    resp,err :=    http.Get("http://www.baidu.com")
    if err!=nil {
        //如果没有获取到url数据 会返回一个error 类型错误
        fmt.Println("error=",err,";")
    }
    defer resp.Body.Close()
    //提取响应头数据
    b,err:=ioutil.ReadAll(resp.Body)
    //将字节切片转成成String 输出
    fmt.Print(string(b))
}
import (  
    "net/http"  
    "net/url"  
    "fmt"  
    "io/ioutil"  
)  
  
func main()  {  
    post()  
}  
func post()  {  
        //这是一个Post 参数会被返回的地址  
        strinUrl:="http://localhost:8080/aaa"  
        resopne,err:=   http.PostForm(strinUrl,url.Values{"num":{"456"}})  
  
        if err !=nil {  
        fmt.Println("err=",err)  
        }  
        defer func() {  
            resopne.Body.Close()  
            fmt.Println("finish")  
        }()  
         //提取数据
        body,err:=ioutil.ReadAll(resopne.Body)  
        if err!=nil {  
            fmt.Println(" post err=",err)  
        }  
        fmt.Println(string(body))  
}

查看更多关于go的入门之路 http Get Post请求的详细内容...

  阅读:100次