简单GET

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. )
  7. func main() {
  8. q, _ := http.Get("http://10.214.200.6:8080/")
  9. //打印状态码
  10. fmt.Println(q.Status)
  11. // 打印请求头
  12. fmt.Println(q.Header)
  13. //打印html源码
  14. html, _ := ioutil.ReadAll(q.Body)
  15. fmt.Println(string(html))
  16. }

image.png

自定义GET

很多时候,我们编写poc的时候,payload都是改变header的,需要自定义header。

  1. package main
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "time"
  7. )
  8. func main() {
  9. //定义请求,但是不发送,只是定义
  10. u := "http://httpbin.org/get"
  11. q, _ := http.NewRequest("GET", u, nil)
  12. //自定义header
  13. q.Header.Set("User-Agent", "ie")
  14. q.Header.Set("Oringin", "test.com")
  15. q.Header.Set("aaa", "aaa")
  16. //定义超时时间,也可以留空
  17. c := http.Client{Timeout: 3 * time.Second}
  18. //获取header
  19. fmt.Println(q.Header)
  20. //发送请求
  21. re, _ := c.Do(q)
  22. //获取html
  23. html, _ := ioutil.ReadAll(re.Body)
  24. fmt.Println(string(html))
  25. }

image.png

简单POST

  1. package main
  2. import (
  3. "net/http"
  4. "strings"
  5. )
  6. func main() {
  7. u := "http://10.214.200.6:8080/"
  8. http.Post(u, "application/x-www-foem-urlencoded", strings.NewReader("a = bb"))
  9. }
  1. package main
  2. import (
  3. "net/http"
  4. "net/url"
  5. )
  6. func main() {
  7. u := "http://10.214.200.6:8080/"
  8. req := url.Values{}
  9. req.Set("a", "bb")
  10. http.PostForm(u, req)
  11. }

自定义POST

  1. package main
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. )
  8. func main() {
  9. u := "http://10.214.200.6:8080/"
  10. payload := map[string]string{
  11. "user": "admin",
  12. "passwd": "admin",
  13. }
  14. buf, _ := json.Marshal(payload) //序列化成byte
  15. //新建请求,不发送
  16. req, _ := http.NewRequest("POST", u, bytes.NewReader(buf))
  17. //自定义header
  18. req.Header.Set("User-Agent", "ie")
  19. req.Header.Set("Cookie", "remenberme=1")
  20. //发送请求
  21. c := http.Client{}
  22. c.Do(req)
  23. fmt.Println(req.Header)
  24. }

忽略证书

在初始化客户端的时候忽略就可以了。

  1. package main
  2. import (
  3. "bytes"
  4. "crypto/tls"
  5. "encoding/json"
  6. "fmt"
  7. "net/http"
  8. )
  9. func main() {
  10. u := "https://www.baidu.com"
  11. payload := map[string]string{
  12. "user": "admin",
  13. "passwd": "admin",
  14. }
  15. buf, _ := json.Marshal(payload) //序列化成byte
  16. //新建请求,不发送
  17. req, _ := http.NewRequest("POST", u, bytes.NewReader(buf))
  18. //自定义header
  19. req.Header.Set("User-Agent", "ie")
  20. req.Header.Set("Cookie", "remenberme=1")
  21. //初始化客户端,并发送请求
  22. //忽略证书
  23. nocert := &http.Transport{
  24. TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
  25. }
  26. c := http.Client{Transport: nocert}
  27. c.Do(req)
  28. fmt.Println(req.Header)
  29. }