普通的get请求

package main

import (
"io/ioutil"
"fmt"
"net/http"
) func main() {
res,_ :=http.Get("https://www.baidu.com/")
defer res.Body.Close()
body,_ := ioutil.ReadAll(res.Body)
fmt.Print(body)
}

带参数的get请求(参数不放在url里)

package main

import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
) func main(){
params := url.Values{}
Url, _:= url.Parse("https://www.baidu.com/")
params.Set("name","zhaofan")
params.Set("age","")
//如果参数中有中文参数,这个方法会进行URLEncode
Url.RawQuery = params.Encode()
urlPath := Url.String()
fmt.Println(urlPath) //等同于https://www.xxx.com?age=23&name=zhaofan
resp,_ := http.Get(urlPath)
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}

get请求添加请求头

package main

import (
"fmt"
"io/ioutil"
"net/http"
) func main() {
client := &http.Client{}
req,_ := http.NewRequest("GET","http://www.xxx.com",nil)
req.Header.Add("name","zhaofan")
req.Header.Add("age","")
resp,_ := client.Do(req)
   defer resp.Body.close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Printf(string(body))
}

post请求

package main

import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
) func main() {
urlValues := url.Values{}
urlValues.Add("name","zhaofan")
urlValues.Add("age","")
resp, _ := http.PostForm("http://www.xxx.com",urlValues)
defer resp.Body.close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}

post请求的另一种方式

package main

import (
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
) func main() {
urlValues := url.Values{
"name":{"zhaofan"},
"age":{""},
}
reqBody:= urlValues.Encode()
resp, _ := http.Post("http://www.xxx.com/post", "text/html",strings.NewReader(reqBody))
defer resp.Body.close()
body,_:= ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}

post请求发送json数据

package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
) func main() {
client := &http.Client{}
data := make(map[string]interface{})
data["name"] = "zhaofan"
data["age"] = ""
bytesData, _ := json.Marshal(data)
req, _ := http.NewRequest("POST","http://www.xxx.com",bytes.NewReader(bytesData))
resp, _ := client.Do(req)
defer resp.Body.close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body)) } 不用client
package main

import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
) func main() {
data := make(map[string]interface{})
data["name"] = "zhaofan"
data["age"] = "23"
bytesData, _ := json.Marshal(data)
resp, _ := http.Post("http://www.xxx.com","application/json", bytes.NewReader(bytesData))
defer resp.Body.close()
body, _ := ioutil.ReadAll(resp.Body)
fmt.Println(string(body))
}
 

说白了,我们记住http.get 和http.post就可以了

go 发送http请求的更多相关文章

  1. Java发送Http请求并获取状态码

    通过Java发送url请求,查看该url是否有效,这时我们可以通过获取状态码来判断. try { URL u = new URL("http://10.1.2.8:8080/fqz/page ...

  2. AngularJs的$http发送POST请求,php无法接收Post的数据解决方案

      最近在使用AngularJs+Php开发中遇到php后台无法接收到来自AngularJs的数据,在网上也有许多解决方法,却都点到即止.多番摸索后记录下解决方法:tips:当前使用的AngularJ ...

  3. Ajax发送POST请求SpringMVC页面跳转失败

    问题描述:因为使用的是SpringMVC框架,所以想使用ModelAndView进行页面跳转.思路是发送POST请求,然后controller层中直接返回相应ModelAndView,但是这种方法不可 ...

  4. 使用HttpClient来异步发送POST请求并解析GZIP回应

    .NET 4.5(C#): 使用HttpClient来异步发送POST请求并解析GZIP回应 在新的C# 5.0和.NET 4.5环境下,微软为C#加入了async/await,同时还加入新的Syst ...

  5. 在发送ajax请求时加时间戳或者随机数去除js缓存

    在发送ajax请求的时候,为了保证每次的都与服务器交互,就要传递一个参数每次都不一样,这里就用了时间戳 大家在系统开发中都可能会在js中用到ajax或者dwr,因为IE的缓存,使得我们在填入相同的值的 ...

  6. HttpUrlConnection发送url请求(后台springmvc)

    1.HttpURLConnection发送url请求 public class JavaRequest { private static final String BASE_URL = "h ...

  7. kattle 发送post请求

    一.简介 kattle是一款国外开源的ETL工具,纯java编写,可以在Window.Linux.Unix上运行,数据抽取高效稳定.它允许你管理来自不同数据库的数据,通过提供一个图形化的用户环境来描述 ...

  8. 【荐】怎么用PHP发送HTTP请求(POST请求、GET请求)?

    file_get_contents版本: <?php /** * 发送post请求 * @param string $url 请求地址 * @param array $post_data pos ...

  9. 使用RestTemplate发送post请求

    最近使用RestTemplate发送post请求,遇到了很多问题,如转换httpMessage失败,中文乱码等,调了好久才找到下面较为简便的方法: RestTemplate restTemplate ...

  10. 【转载】JMeter学习(三十六)发送HTTPS请求

    Jmeter一般来说是压力测试的利器,最近想尝试jmeter和BeanShell进行接口测试.由于在云阅读接口测试的过程中需要进行登录操作,而登录请求是HTTPS协议.这就需要对jmeter进行设置. ...

随机推荐

  1. 参数化解决sql注入

    用DynamicParameters: string where = " where a.is_deleted=0 and a.bvent_id=@bventId and au.user_t ...

  2. echarts改变图例位置

    只需要legend属性中修改如下几个示数即可: legend:{ orient:"horizontal", x:'right', y:' center', width:'100', ...

  3. #import,#include与@class的区别

    1.#include是C中用来引用文件的关键字,而#import是obj-c中用来代替include的关键字.#import可以确保同一个文件只能被导入一次,从而避免了使用#include容易引起的重 ...

  4. linux下创建svn仓库及用户

    1 Linux下创建svn仓库 1.1 启动SVN服务 svnserve -d -r  /SVNRootDirectry 其中SVNRootDirectry是你的SVN 根目录,例如192.85.1. ...

  5. 2018-2-13-win10-uwp-BadgeLogo-颜色

    title author date CreateTime categories win10 uwp BadgeLogo 颜色 lindexi 2018-2-13 17:23:3 +0800 2018- ...

  6. 开源安全:PE分析

    https://github.com/JusticeRage/Manalyze.git https://github.com/JusticeRage/Manalyze https://www.free ...

  7. spring cloud学习一--Eureka服务注册与发现

    spring cloud Eureka是基于Netflix Eureka服务发现注册产品的二次封装,它提供了服务注册功能(Service Registry)和服务发现功能(Service Discov ...

  8. springboot中使用RabbitMq

    转载:http://www.ityouknow.com/springboot/2016/11/30/spring-boot-rabbitMQ.html RabbitMQ 即一个消息队列,主要是用来实现 ...

  9. 浅析 http 接口

     一.HTTP接口 Http协议是建立在TCP协议基础之上的,当浏览器需要从服务器获取网页数据的时候,会发出一次Http请求.Http会通过TCP建立起一个到服务器的连接通道,当本次请求需要的数据完毕 ...

  10. Python"sorted()"和".sort()"的区别

    sorted(A-LIST)会返回一个新的object,不改变**A-LIST*本身. A-LIST.sort()会直接改变A-List,不产生新的object.