Go中的http使用

package main

import (
"fmt"
"net/http"
"io/ioutil"
"strings"
"net/url"
) func httpGet(){
resp, err := http.Get("http://www.baidu.com")
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))
} func httpPost(){
resp, err := http.Post("http://www.baidu.com",
"application/x-www-form-urlencoded",
strings.NewReader("name=zzr"))
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))
} func httpPostForm(){
resp, err := http.PostForm("http://www.baidu.com",
url.Values{"key":{"value"}, "id":{"123"}}) 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))
} func httpDo(){
client := &http.Client{} req, err := http.NewRequest("POST", "http://www.baidu.com",strings.NewReader("name=zzr"))
if err != nil{
fmt.Println(err)
return
}
req.Header.Set("Content-Type", "application/x-www.form-urlencoded")
req.Header.Set("Cookie", "name=ben") resp, err := client.Do(req) defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println(err)
return
}
fmt.Println(string(body))
}
func main(){
httpPost()
}

最新文章

  1. JQ中的方法、事件及动画
  2. ORACLE DELETE数据慢的案例
  3. redis之理解
  4. Windows Azure Cloud Service (36) 在Azure Cloud Service配置SSL证书
  5. 两种CSS3圆环进度条详解
  6. jQuery动态产生的铵钮怎样实现事件处理
  7. 在ScrollView下加入的组件,不能自动扩展到屏幕高度
  8. Windows服务的手动添加和删除方法
  9. jquery stop
  10. IIS大数据请求设置方法
  11. A Tour of Go Making slices
  12. JS匿名执行函数
  13. UDP vs. TCP
  14. When Startup Disk is Full
  15. plsql中文乱码问题方案解决
  16. 用js脚本一键下载网页所有图片
  17. php留言板的实现
  18. maven项目导入依赖jar包并打包为可运行的jar包
  19. GO语言系列(二)- 基本数据类型和操作符
  20. unity打包安卓应用及生成签名

热门文章

  1. [Err] 1214 - The used table type doesn't support FULLTEXT indexes
  2. listview加载显示图片
  3. 多目标规划——fgoalattain
  4. 用selenium爬动态网页
  5. mybatis学习记录六——一对一、一对多和多对多查询
  6. 使用Vue做评论+localStorage存储(js模块化)
  7. 【luogu P1314 聪明的质监员】 题解
  8. 【题解】洛谷P1731 [NOI1999] 生日蛋糕(搜索+剪枝)
  9. oracle序列中cache和nocache
  10. element 表单的input循环生成,并可单个input失去焦点单个验证并保存; (多个表单实例)