最近需要自动注册facebook帐号,所以试着使用golang来开发程序,通过fiddler来分析数据的提交是否跟浏览器一致
package main
import (
"fmt"
"log"
"net/http"
"os"
"net/http/cookiejar"
"net/url"
)
func main() {
var gCurCookies []*http.Cookie
var gCurCookieJar *cookiejar.Jar
gCurCookies = nil
//var err error;
gCurCookieJar, _ = cookiejar.New(nil)
log.Println("start request facebook.com")
//直接本地的shadowsocks, 连接facebook
/*dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:1080", nil, proxy.Direct)
if err != nil {
log.Println(err)
return
}
*/
//连接到fiddler,而fiddler不支持sock5代理,所以又需要proxifier,实现全局的代理
httpTransport := &http.Transport{
Proxy: func(r *http.Request) (*url.URL, error) { return url.Parse("http://127.0.0.1:8888") }, //
}
httpClient := &http.Client{
Transport: httpTransport,
Jar: gCurCookieJar, //新的请求时,可以指定相同的jar,就可以发送cookies
}
//httpTransport.Dial = dialer.Dial
req, err := http.NewRequest("GET", "https://m.facebook.com", nil)
if err != nil {
log.Println(err)
return
}
req.Header.Add("Accept-Language", " zh-CN,zh;q=0.9")
req.Header.Add("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 8_3 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12F70 Safari/600.1.4")
req.Header.Add("Upgrade-Insecure-Requests", "1")
req.Header.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8")
resp, err := httpClient.Do(req)
if err != nil {
fmt.Fprintln(os.Stderr, "can't GET page:", err)
os.Exit(3)
}
defer resp.Body.Close()
log.Printf("httpResp.Header=%s", resp.Header)
log.Printf("httpResp.Status=%s", resp.Status)
//fmt.Println(string(b))
gCurCookies = gCurCookieJar.Cookies(req.URL)
for i := 0; i < len(gCurCookies); i++ {
var curCk *http.Cookie = gCurCookies[i]
log.Printf("Cookie [%d]", i)
log.Printf("Name\t=%s", curCk.Name)
log.Printf("Value\t=%s", curCk.Value)
log.Printf("Path\t=%s", curCk.Path)
log.Printf("Domain\t=%s", curCk.Domain)
// 0001-01-01 00:00:00 +0000 UTC
// 因为jar cookies 按以下方式返回
/*
for _, e := range selected {
//并没有复制expires
cookies = append(cookies, &http.Cookie{Name: e.Name, Value: e.Value})
}
*/
log.Printf("Expires\t=%s", curCk.Expires)
log.Printf("RawExpires=%s", curCk.RawExpires)
log.Printf("MaxAge\t=%d", curCk.MaxAge)
log.Printf("Secure\t=%t", curCk.Secure)
log.Printf("HttpOnly=%t", curCk.HttpOnly)
log.Printf("Raw\t=%s", curCk.Raw)
log.Printf("Unparsed=%s", curCk.Unparsed)
}
}
fidder开启代理