golang要请求远程网页,可以使用net/http包中的client提供的方法实现。查看了官方网站有一些示例,没有太全面的例子,于是自己整理了一下。
get请求
get请求可以直接http.Get方法,非常简单。
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方式
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方法
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方法。
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代码示例
<!-- 336x280 --> <iframe id="aswift_1" style="left: 0px; top: 0px; position: absolute;" name="aswift_1" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" width="336" height="280"></iframe>
相关推荐
GET 和 POST 是我们最常用的两种请求方式,今天讲一讲如何在 golang 服务中,正确接收这两种请求的参数信息。 处理GET请求 1.1 接收GET请求 //接收GET请求 func Get(writer http.ResponseWriter , request *...
golang_http_client
主要给大家介绍了关于Go语言中利用http发起Get和Post请求的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
今天小编就为大家分享一篇Golang 使用http Client下载文件的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
本文实例讲述了Go语言服务器开发实现最简单HTTP的GET与POST接口。分享给大家供大家参考。具体分析如下: Go语言提供了http包,可以很轻松的开发http接口。以下为示例代码: 代码如下:package webserver import...
使用标准库http来实现 package tools import ( io/ioutil net/http ) func Get(url string)string{ res, err :=http.Get(url) if err !...以上就是Golang发送http GET请求的示例代码的详细内容,更
这里我从了Golang SDK自带的net/http包提供的HTTP相关API,虽然本身提供了http.PostForm()、http.Post()以及http.Get()封装好的方法,但是在处理HTTPrequest的header和cookie处理缺乏灵活性,所以我重新将net/http...
主要介绍了GoLang之使用Context控制请求超时的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
websocket-golang-chat, 使用golang和 web sockets进行简单聊天 这是一个使用golang和 web sockets的简单网络聊天应用程序的演示。这需要去 web socket包,以获取包go get code.google.com/p/go.net/websocket获取...
golang tcp http搭建与通信实例,完全由本人一手编写,相对简单易懂
戈兹一个在golang中使用的出色的HTTP请求库。 受到启发安装go get -u github.com/idoubi/goz文献资料API文档可在以下位置找到: : 基本用法package mainimport ( "github.com/idoubi/goz")func main () { cli := goz...
Go示例学--通过示例学习Golang语言,比较实用。边学边实战,浅显易懂,golang初学者容易入门首选。
2. 发送POST和Get请求 package http import ( bytes encoding/json io io/ioutil net/http time ) // 发送GET请求 // url: 请求地址 // response: 请求返回的内容 func Get(url string) string { // 超时...
简单配置文件后一次性对多个地址发起多次请求, 可最多100线程同时发送请求
golang 使用go-ora 连接数据库 示例代码。 不用安装 cgo,不用安装mingw,也不用oracle客户端
使用Golang 搭建http web服务器
httpstat一个Go包能够跟踪HTTP延迟信息适用于任何golang的HTTP请求