http apis
http.Get(url string) (resp *Response, err error)
http.Head(url string) (resp *Response, err error)
http.Post(url string, bodyType string, body io.Reader) (resp *Response, err error)
POST
package main
import (
"fmt"
"log"
"net/http"
"net/url"
"strings"
)
func main() {
form := url.Values{}
form.Add("username","srat")
form.Add("password","123456")
url := "http://xxx/login"
contentType := "application/x-www-form-urlencoded"
if res,err := http.Post(url,contentType,strings.NewReader(form.Encode()));err!=nil{
log.Fatal("conn err")
defer res.Body.Close()
}else {
resp,_ := ioutil.Readall(res.Body)
fmt.Println(string())
}
}
或者
form := url.Values{}
form.Add("username","srat")
form.Add("password","123456")
url := "http://xxx/login"
//contentType := "application/x-www-form-urlencoded"
if res,err := http.PostForm(url,form);err!=nil{
log.Fatal("conn err")
}else {
resp,_ := ioutil.Readall(res.Body)
fmt.Println(string(resp))
}
Using Structured Response Parsing
response结构体
type Response struct {
Status string // e.g. "200 OK"
StatusCode int // e.g. 200
Proto string // e.g. "HTTP/1.0"
ProtoMajor int // e.g. 1
ProtoMinor int // e.g. 0
Header Header
Body io.ReadCloser
ContentLength int64
TransferEncoding []string
Close bool
Uncompressed bool
Trailer Header
Request *Request
TLS *tls.ConnectionState
}
解析json数据
解析Fofa返回json
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
)
type info struct {
Mode string `json:"mode"` // tag 即解析json的别名
Error bool `json:"error"`
Query string `json:"query"`
Page int `json:"page"`
Size int `json:"size"`
Results [][]string `json:"results"`
}
var (
key = "xxxxxxxxxxxxxxxxxxxxxxxxxxx"
mail = "xxxxxxxxxxxxxxx"
query string
)
func main() {
a := []byte("domain=nuist.edu.cn")
query = base64.StdEncoding.EncodeToString(a) // base64
url := fmt.Sprintf("https://fofa.so/api/v1/search/all?email=%s&key=%s&qbase64=%s",mail,key,query)
res,err := http.Get(url)
var i info
if err != nil{
log.Fatal(err)
os.Exit(0)
}else {
if resp,err := ioutil.ReadAll(res.Body);err!=nil{
log.Fatal("err")
os.Exit(0)
}else {
fmt.Println(string(resp))
if err = json.Unmarshal(resp,&i);err!=nil{ // json反序列化
fmt.Println(err)
os.Exit(0)
}
}
results :=i.Results
for _,v :=range (results){
fmt.Println(v[0])
}
}
}
这里需要注意的点比较多。声名接收json反序列化结构体的时候,成员变量首字符要大写,否则就会出现反序列化赋值无法成功。还有就是Unmarshal接收参数为指针,不能直接传入变量实体。这是个非常简单的Demo版本,好在Fofa的一切查询都是基于语法,后期想要修改也比较容易。