Go语言net/http包的使用
net/http包是golang 内置标准库之一
Get请求 get请求http.Get方法
func main() {
resp, err := http.Get("http://www.baidu.com")
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()//延迟关闭resp.Body 它是一个ioReader
headers := resp.Header//获得响应头
for k, v := range headers {
fmt.Printf("k=%v, v=%v\n", k, v)
}
fmt.Printf("resp status %s,statusCode %d\n", resp.Status, resp.StatusCode)//响应状态
fmt.Printf("resp Proto %s\n", resp.Proto)//协议
fmt.Printf("resp content length %d\n", resp.ContentLength)//响应体长度
fmt.Printf("resp transfer encoding %v\n", resp.TransferEncoding)//编码表
fmt.Printf("resp Uncompressed %t\n", resp.Uncompressed)
fmt.Println(reflect.TypeOf(resp.Body)) // *http.gzipReader//响应体,是主要解析的内容
buf := bytes.NewBuffer(make([]byte, 0, 512))
length, _ := buf.ReadFrom(resp.Body)
fmt.Println(len(buf.Bytes()))
fmt.Println(length)
fmt.Println(string(buf.Bytes()))
}}
Do方法 在请求的时候设置头参数 cookie之类的数据 使用http.Do 方法
func main() {
client := &http.Client{}
req, err := http.NewRequest("POST", "http://www.maimaiche.com/loginRegister/login.do",
strings.NewReader("mobile=xxxxxxxxx&isRemberPwd=1"))
if err != nil {
log.Println(err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
resp, err := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
fmt.Println(resp.Header.Get("Content-Type")) //application/json;charset=UTF-8
type Result struct {
Msg string
Status string
Obj string
}
result := &Result{}
json.Unmarshal(body, result) //解析json字符串
if result.Status == "1" {
fmt.Println(result.Msg)
} else {
fmt.Println("login error")
}
fmt.Println(result)
}
Post请求 如果使用http POST方法可以直接使用http.Post 或 http.PostForm
func main() {
resp, err := http.Post("http://www.maimaiche.com/loginRegister/login.do",
"application/x-www-form-urlencoded",
strings.NewReader("mobile=xxxxxxxxxx&isRemberPwd=1"))
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
PostForm方法 用以提交表单并获得响应
func main() {
postParam := url.Values{
"mobile": {"xxxxxx"},
"isRemberPwd": {"1"},
}
resp, err := http.PostForm("http://www.maimaiche.com/loginRegister/login.do", postParam)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
net/http包是golang 内置标准库之一
Get请求 get请求http.Get方法
func main() {
resp, err := http.Get("http://www.baidu.com")
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()//延迟关闭resp.Body 它是一个ioReader
headers := resp.Header//获得响应头
for k, v := range headers {
fmt.Printf("k=%v, v=%v\n", k, v)
}
fmt.Printf("resp status %s,statusCode %d\n", resp.Status, resp.StatusCode)//响应状态
fmt.Printf("resp Proto %s\n", resp.Proto)//协议
fmt.Printf("resp content length %d\n", resp.ContentLength)//响应体长度
fmt.Printf("resp transfer encoding %v\n", resp.TransferEncoding)//编码表
fmt.Printf("resp Uncompressed %t\n", resp.Uncompressed)
fmt.Println(reflect.TypeOf(resp.Body)) // *http.gzipReader//响应体,是主要解析的内容
buf := bytes.NewBuffer(make([]byte, 0, 512))
length, _ := buf.ReadFrom(resp.Body)
fmt.Println(len(buf.Bytes()))
fmt.Println(length)
fmt.Println(string(buf.Bytes()))
}}
Do方法 在请求的时候设置头参数 cookie之类的数据 使用http.Do 方法
func main() {
client := &http.Client{}
req, err := http.NewRequest("POST", "http://www.maimaiche.com/loginRegister/login.do",
strings.NewReader("mobile=xxxxxxxxx&isRemberPwd=1"))
if err != nil {
log.Println(err)
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
resp, err := client.Do(req)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
return
}
fmt.Println(resp.Header.Get("Content-Type")) //application/json;charset=UTF-8
type Result struct {
Msg string
Status string
Obj string
}
result := &Result{}
json.Unmarshal(body, result) //解析json字符串
if result.Status == "1" {
fmt.Println(result.Msg)
} else {
fmt.Println("login error")
}
fmt.Println(result)
}
Post请求 如果使用http POST方法可以直接使用http.Post 或 http.PostForm
func main() {
resp, err := http.Post("http://www.maimaiche.com/loginRegister/login.do",
"application/x-www-form-urlencoded",
strings.NewReader("mobile=xxxxxxxxxx&isRemberPwd=1"))
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
PostForm方法 用以提交表单并获得响应
func main() {
postParam := url.Values{
"mobile": {"xxxxxx"},
"isRemberPwd": {"1"},
}
resp, err := http.PostForm("http://www.maimaiche.com/loginRegister/login.do", postParam)
if err != nil {
fmt.Println(err)
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
尊贵的董事大人
英文标题不为空时 视为本栏投稿
需要关键字 描述 英文标题