Go语言中的HTTP
2024-08-24 20:14:36
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()
}
最新文章
- JQ中的方法、事件及动画
- ORACLE DELETE数据慢的案例
- redis之理解
- Windows Azure Cloud Service (36) 在Azure Cloud Service配置SSL证书
- 两种CSS3圆环进度条详解
- jQuery动态产生的铵钮怎样实现事件处理
- 在ScrollView下加入的组件,不能自动扩展到屏幕高度
- Windows服务的手动添加和删除方法
- jquery stop
- IIS大数据请求设置方法
- A Tour of Go Making slices
- JS匿名执行函数
- UDP vs. TCP
- When Startup Disk is Full
- plsql中文乱码问题方案解决
- 用js脚本一键下载网页所有图片
- php留言板的实现
- maven项目导入依赖jar包并打包为可运行的jar包
- GO语言系列(二)- 基本数据类型和操作符
- unity打包安卓应用及生成签名
热门文章
- [Err] 1214 - The used table type doesn't support FULLTEXT indexes
- listview加载显示图片
- 多目标规划——fgoalattain
- 用selenium爬动态网页
- mybatis学习记录六——一对一、一对多和多对多查询
- 使用Vue做评论+localStorage存储(js模块化)
- 【luogu P1314 聪明的质监员】 题解
- 【题解】洛谷P1731 [NOI1999] 生日蛋糕(搜索+剪枝)
- oracle序列中cache和nocache
- element 表单的input循环生成,并可单个input失去焦点单个验证并保存; (多个表单实例)