简单GET
package main
import (
"fmt"
"io/ioutil"
"net/http"
)
func main() {
q, _ := http.Get("http://10.214.200.6:8080/")
//打印状态码
fmt.Println(q.Status)
// 打印请求头
fmt.Println(q.Header)
//打印html源码
html, _ := ioutil.ReadAll(q.Body)
fmt.Println(string(html))
}
自定义GET
很多时候,我们编写poc的时候,payload都是改变header的,需要自定义header。
package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
)
func main() {
//定义请求,但是不发送,只是定义
u := "http://httpbin.org/get"
q, _ := http.NewRequest("GET", u, nil)
//自定义header
q.Header.Set("User-Agent", "ie")
q.Header.Set("Oringin", "test.com")
q.Header.Set("aaa", "aaa")
//定义超时时间,也可以留空
c := http.Client{Timeout: 3 * time.Second}
//获取header
fmt.Println(q.Header)
//发送请求
re, _ := c.Do(q)
//获取html
html, _ := ioutil.ReadAll(re.Body)
fmt.Println(string(html))
}
简单POST
package main
import (
"net/http"
"strings"
)
func main() {
u := "http://10.214.200.6:8080/"
http.Post(u, "application/x-www-foem-urlencoded", strings.NewReader("a = bb"))
}
package main
import (
"net/http"
"net/url"
)
func main() {
u := "http://10.214.200.6:8080/"
req := url.Values{}
req.Set("a", "bb")
http.PostForm(u, req)
}
自定义POST
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
)
func main() {
u := "http://10.214.200.6:8080/"
payload := map[string]string{
"user": "admin",
"passwd": "admin",
}
buf, _ := json.Marshal(payload) //序列化成byte
//新建请求,不发送
req, _ := http.NewRequest("POST", u, bytes.NewReader(buf))
//自定义header
req.Header.Set("User-Agent", "ie")
req.Header.Set("Cookie", "remenberme=1")
//发送请求
c := http.Client{}
c.Do(req)
fmt.Println(req.Header)
}
忽略证书
在初始化客户端的时候忽略就可以了。
package main
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
)
func main() {
u := "https://www.baidu.com"
payload := map[string]string{
"user": "admin",
"passwd": "admin",
}
buf, _ := json.Marshal(payload) //序列化成byte
//新建请求,不发送
req, _ := http.NewRequest("POST", u, bytes.NewReader(buf))
//自定义header
req.Header.Set("User-Agent", "ie")
req.Header.Set("Cookie", "remenberme=1")
//初始化客户端,并发送请求
//忽略证书
nocert := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
c := http.Client{Transport: nocert}
c.Do(req)
fmt.Println(req.Header)
}