golang使用http client发起get和post请求示例
【转自 http://www.01happy.com/golang-http-client-get-and-post/ 】
get请求
get请求可以直接http.Get方法,非常简单。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
func httpGet() { resp, err := http.Get( "http://www.01happy.com/demo/accept.php?id=1" ) if err != nil { // handle error } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { // handle error } fmt.Println(string(body)) } |
post请求
一种是使用http.Post方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
func httpPost() { resp, err := http.Post( "http://www.01happy.com/demo/accept.php" , "application/x-www-form-urlencoded" , strings.NewReader( "name=cjb" )) if err != nil { fmt.Println(err) } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { // handle error } fmt.Println(string(body)) } |
Tips:使用这个方法的话,第二个参数要设置成”application/x-www-form-urlencoded”,否则post参数无法传递。
一种是使用http.PostForm方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
func httpPostForm() { resp, err := http.PostForm( "http://www.01happy.com/demo/accept.php" , url.Values{ "key" : { "Value" }, "id" : { "123" }}) if err != nil { // handle error } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { // handle error } fmt.Println(string(body)) } |
复杂的请求
有时需要在请求的时候设置头参数、cookie之类的数据,就可以使用http.Do方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
func httpDo() { client := &http.Client{} req, err := http.NewRequest( "POST" , "http://www.01happy.com/demo/accept.php" , strings.NewReader( "name=cjb" )) if err != nil { // handle error } req.Header.Set( "Content-Type" , "application/x-www-form-urlencoded" ) req.Header.Set( "Cookie" , "name=anny" ) resp, err := client.Do(req) defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { // handle error } fmt.Println(string(body)) } |
同上面的post请求,必须要设定Content-Type为application/x-www-form-urlencoded,post参数才可正常传递。
如果要发起head请求可以直接使用http client的head方法,比较简单,这里就不再说明。
完整代码示例文件下载:golang_http_client发起get和post代码示例
golang使用http client发起get和post请求示例的更多相关文章
- 【SpringCloud Eureka源码】从Eureka Client发起注册请求到Eureka Server处理的整个服务注册过程(下)
目录 一.Spring Cloud Eureka Server自动配置及初始化 @EnableEurekaServer EurekaServerAutoConfiguration - 注册服务自动配置 ...
- 可能会搞砸你的面试:你知道一个TCP连接上能发起多少个HTTP请求吗?
本文由原作者松若章原创发布,作者主页:zhihu.com/people/hrsonion/posts,感谢原作者的无私分享. 1.引言 一道经典的面试题是:从 URL 在浏览器被被输入到页面展现的过程 ...
- 浏览器发起Get,Post请求时候传递的参数编码问题
浏览器发起Get,Post请求时候传递的参数编码问题 最近开发一个网站的时候,用了很多ajax方法,在页面发起Get,post请求,中间自然捎带有很多参数,有中文,有英文,英文一般是不存在编码问题的, ...
- Python向PHP发起GET与POST请求
CloudB项目中到PHP开发WEB管理端,用Python开发服务控制端,在项目中Python的服务控制端有时候须要主动连接PHP的WEB管理端下载或上传配置參数或数据信息,这里採用的原理是Pytho ...
- python 爬虫 基于requests模块发起ajax的post请求
基于requests模块发起ajax的post请求 需求:爬取肯德基餐厅查询http://www.kfc.com.cn/kfccda/index.aspx中指定某个城市地点的餐厅数据 点击肯德基餐厅查 ...
- python 爬虫 基于requests模块发起ajax的get请求
基于requests模块发起ajax的get请求 需求:爬取豆瓣电影分类排行榜 https://movie.douban.com/中的电影详情数据 用抓包工具捉取 使用ajax加载页面的请求 鼠标往下 ...
- Java基础/发起http和https请求
Java中发起http和https请求 一般调用外部接口会需要用到http和https请求. 本案例为:前后端完全分离,前端框架(React+Mobx+Nornj),后端(Go语言). 面临问题:跨域 ...
- Elasticsearch High Level Rest Client 发起请求的过程分析
本文讨论的是JAVA High Level Rest Client向ElasticSearch6.3.2发送请求(index操作.update.delete--)的一个详细过程的理解,主要涉及到Res ...
- DNS Tunneling及相关实现——总之,你发起攻击都需要一个DNS server,下载一些工具作为client发起数据,server收集数据并响应
摘自:http://www.freebuf.com/sectool/112076.html DNS Tunneling,是隐蔽信道的一种,通过将其他协议封装在DNS协议中传输建立通信.因为在我们的网络 ...
随机推荐
- Laravel5.5 的 Homestead 开发环境部署
首先明白以下几个概念 VirtualBox -- Oracle 公司的虚拟机软件, 能运行在当前大部分流行的系统上; Vagrant 提供一种命令行接口, 允许自动化安装虚拟机, 并且因为是脚本编写 ...
- Nginx的启动(start),停止(stop)命令
http://blog.csdn.net/u010739551/article/details/51654859 查看Nginx的版本号:nginx -V 启动Nginx:start nginx 快速 ...
- 基于VUE选择上传图片并在页面显示(图片可删除)
demo例子: 依赖文件 : http://files.cnblogs.com/files/zhengweijie/jquery.form.rar HTML文本内容: <template> ...
- php对数组进行分页
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...
- SSL协议之数据加密过程详解
前言 总括: 原文博客地址:SSL协议之数据加密过程详解 知乎专栏&&简书专题:前端进击者(知乎)&&前端进击者(简书) 博主博客地址:Damonare的个人博客 生活 ...
- 【编程技巧】java不使用第三个变量处理两个变量的交换
public class SwapNum {public static void main(String[] args) {doSwapNum();}public static void doSwap ...
- 【开发技术】 Air display的介绍与使用
原理是,通过ipad与电脑连接的同一网络,然后把你的ipad变成电脑的屏幕 安装方法:1, 2, 3,
- h5开发安卓软键盘遮挡解决方案
//处理input focus时被键盘遮挡问题 inputFocus:function(){ if(/Android [4-6]/.test(navigator.appVersion)) { wind ...
- Java中 equals() 和 == 的区别
1)对于==,如果作用于基本数据类型的变量,则直接比较其存储的 "值"是否相等: 如果作用于引用类型的变量,则比较的是所指向的对象的地址 2)对于equals方法,注意:equal ...
- Python 使用Pillow模块生成验证码
1.安装 pip3 install pillow 2.使用步骤 生成验证码和验证字符串 绘制图片,将验证码放入session中 将图片返回给页面 3.代码demo #!/usr/bin/env pyt ...