本文整理匯總了Golang中net/http/cookiejar.Jar.SetCookies方法的典型用法代碼示例。如果您正苦於以下問題:Golang Jar.SetCookies方法的具體用法?Golang Jar.SetCookies怎麽用?Golang Jar.SetCookies使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類net/http/cookiejar.Jar
的用法示例。
在下文中一共展示了Jar.SetCookies方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: AddCookies
func (sd *SessionData) AddCookies(cookies *cookiejar.Jar) {
cookies.SetCookies(cookiePath, []*http.Cookie{
&http.Cookie{
Name: "mobileClientVersion",
Value: "0 (2.1.3)",
Path: "/",
Domain: ".steamcommunity.com",
},
&http.Cookie{
Name: "mobileClient",
Value: "android",
Path: "/",
Domain: ".steamcommunity.com",
},
&http.Cookie{
Name: "Steam_Language",
Value: "english",
Path: "/",
Domain: ".steamcommunity.com",
},
&http.Cookie{
Name: "steamid",
Value: strconv.FormatUint(sd.SteamID, 10),
Path: "/",
Domain: ".steamcommunity.com",
},
&http.Cookie{
Name: "steamLogin",
Value: sd.SteamLogin,
Path: "/",
Domain: ".steamcommunity.com",
HttpOnly: true,
},
&http.Cookie{
Name: "steamLoginSecure",
Value: sd.SteamLoginSecure,
Path: "/",
Domain: ".steamcommunity.com",
Secure: true,
HttpOnly: true,
},
&http.Cookie{
Name: "dob",
Value: "",
Path: "/",
Domain: ".steamcommunity.com",
},
&http.Cookie{
Name: "sessionid",
Value: sd.SessionID,
Path: "/",
Domain: ".steamcommunity.com",
},
})
}
示例2: login
func login(privKey box.PrivateKey) error {
var err error
var challenge []byte
var ok bool
var jar *cookiejar.Jar
c := make(map[string]string)
// Step1: request challenge
resp, err := http.Get(fmt.Sprintf("%s/%s", api["auth"].String(), username))
if err != nil {
return err
}
d := json.NewDecoder(resp.Body)
defer resp.Body.Close()
d.Decode(&c)
data := decodeBase64(c["challenge"])
if box.BoxIsSigned(data) {
log.Println("opening signed box...")
challenge, ok = box.OpenAndVerify(data, privKey, srvKey)
} else {
log.Println("opening box...")
challenge, ok = box.Open(data, privKey)
}
if !ok {
log.Println("unboxing returned not ok")
return &internalError{E_AUTH}
}
// Step2: validate challenge
step2, err := json.Marshal(&map[string]string{"id": c["id"], "challenge": base64.StdEncoding.EncodeToString(challenge)})
if err != nil {
return err
}
resp, err = http.Post(api["auth"].String(), jsonMime, strings.NewReader(string(step2)))
if err != nil {
return err
}
if resp.StatusCode == http.StatusUnauthorized {
return &internalError{E_AUTH}
}
if resp.StatusCode != http.StatusOK {
return &internalError{E_SERVER}
}
jar, err = cookiejar.New(nil)
if err != nil {
log.Fatalf("Failed opening cookiejar: %v\n", err)
}
jar.SetCookies(api["/"], resp.Cookies())
myId, _ = strconv.ParseInt(c["id"], 10, 64)
client = &http.Client{Jar: jar}
return nil
}
示例3: getFinalURL
func getFinalURL(jar *cookiejar.Jar, addr *url.URL, level int) (*url.URL, error) {
if flDebug {
log.Printf("new request (level %d): %s", level, addr.String())
}
if level == 0 {
return nil, fmt.Errorf("too many redirects")
}
if addr.Scheme != "http" && addr.Scheme != "https" {
return nil, fmt.Errorf("unsupported url scheme: %s", addr.Scheme)
}
req, err := http.NewRequest("GET", addr.String(), nil)
if err != nil {
return nil, fmt.Errorf("request create err: %s", err)
}
cookies := jar.Cookies(addr)
for _, cookie := range cookies {
if flDebug {
log.Printf("add cookie: %s", cookie.String())
}
req.AddCookie(cookie)
}
req.Header.Set("User-Agent", userAgent)
resp, err := http.DefaultTransport.RoundTrip(req)
if err != nil {
if uErr, ok := err.(*url.Error); !ok || uErr.Err != errStopRedirect {
return nil, fmt.Errorf("request err: %s", err)
}
}
defer resp.Body.Close()
ct := strings.ToLower(resp.Header.Get("Content-Type"))
if flDebug {
log.Printf("response code: %d, content-type %s", resp.StatusCode, ct)
}
jar.SetCookies(resp.Request.URL, resp.Cookies())
if resp.StatusCode == 301 || resp.StatusCode == 302 || resp.StatusCode == 303 || resp.StatusCode == 307 {
location := resp.Header.Get("Location")
addr, err = url.Parse(location)
if err != nil {
return nil, err
}
return getFinalURL(jar, addr, level-1)
}
if !strings.HasPrefix(ct, "text/html") {
if flDebug {
log.Printf("not html content-type: %s", ct)
}
return resp.Request.URL, err
}
buf := &bytes.Buffer{}
_, err = io.CopyN(buf, resp.Body, 1024*1024*10)
if err != nil && err != io.EOF {
return nil, err
}
data := bytes.ToLower(buf.Bytes())
if flDebug {
log.Printf("html: %s", string(data))
}
metaRedirect, err := findMetaRedirect(data)
if err != nil {
return nil, fmt.Errorf("meta redirect err: %s", err)
}
if metaRedirect == nil {
return addr, nil
}
if flDebug {
log.Printf("html meta redirect: %s", metaRedirect)
}
return getFinalURL(jar, metaRedirect, level-1)
}