普通的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. C#设计模式:组合模式(Composite Pattern)

    一,C#设计模式:组合模式(Composite Pattern) using System; using System.Collections.Generic; using System.Linq; ...

  2. JavaFX程序初次运行创建数据库并执行建表SQL

    在我的第一个JavaFX程序完成安装的时候才突然发现,不能要用这个软件还要手动执行Sql来建表吧? 于是我的想法是在Main程序中执行时检测数据库连接状况,如果没有检测到数据库或者连接异常,那么出现错 ...

  3. web笔记全

    1.项目流程与数据库 1.课程体系 阶段1(服务器开发): 项目导入/数据库/JS基础/NodeJS 阶段2(前端核心技术): HTML/AJAX/CSS/bootstrap 阶段3(前端进阶技术): ...

  4. Synchronized和ReentranLock的比较

    并发编程最容易遇到的问题就是就是安全问题,因此解决方式有两种 使用同步方法或同步代码块(Synchronized关键字) 使用锁机制(ReentranLock) 同步方法和同步代码块(Synchron ...

  5. bzoj4987 Tree 树上背包

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4987 题解 一道还不错的题咯. 很容易发现一个结论:这 \(k\) 个点构成的一定是一个连通块 ...

  6. HashMap、HashTable、ConcurrentHashMap区别

    HashMap与HashTable区别 HashMap与ConcurrentHashMap区别 1.HashMap与HashTable的区别 HashMap线程不安全,HashTable线程安全 Ha ...

  7. 并行流水线--求 (B+C)*B/2

    public class Msg { public double i; public double j; public String orgStr = null; } import java.util ...

  8. 如何在pycharm中进入shell脚本调试代码

    首先在Teramal终端 输入python manage.py shell 然后进行下图操作来调试代码

  9. MySQL执行计划之EXPLAIN基本解释说明

    一.EXPLAIN使用潜规则 explain + sql语句 例如: EXPLAIN SELECT * FROM `t_user`; 二. 表头字段详解 (1) id-----> 表的读取顺序 ...

  10. 4412 Linux设备总线

    总线_设备_驱动注册流程详解 注册流程图 • 设备一般都需要先注册,才能注册驱动– 现在越来越多的热拔插设备,反过来了.先注册驱动,设备来了再注册 设备 • 本节使用的命令– 查看总线的命令#ls / ...