本文整理汇总了Golang中net/http.CookieJar类的典型用法代码示例。如果您正苦于以下问题:Golang CookieJar类的具体用法?Golang CookieJar怎么用?Golang CookieJar使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CookieJar类的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: parseCookies
// parseCookies set the sessionCookie via Javascript evaluation
func parseCookies(base *url.URL, js string, cookies http.CookieJar) {
vm := otto.New()
if result, err := vm.Run(js + "\nWEBVAR_JSONVAR_WEB_SESSION.WEBVAR_STRUCTNAME_WEB_SESSION[0].SESSION_COOKIE"); err == nil {
cookie, _ := result.ToString()
cookies.SetCookies(base, []*http.Cookie{{Name: "SessionCookie", Value: cookie}})
} else {
log.Fatalf("Error: %s\n", err)
}
}
示例2: SetCookie
// SetCookie sets a cookie for the given URL on the given cookie jar
// that will holds the given macaroon slice. The macaroon slice should
// contain a single primary macaroon in its first element, and any
// discharges after that.
func SetCookie(jar http.CookieJar, url *url.URL, ms macaroon.Slice) error {
cookie, err := NewCookie(ms)
if err != nil {
return errgo.Mask(err)
}
// TODO verify that setting this for the URL makes it available
// to all paths under that URL.
jar.SetCookies(url, []*http.Cookie{cookie})
return nil
}
示例3: CreateSessionIDer
// CreateSessionIDer provides a default implement for extracting the session from a cookie jar
func CreateSessionIDer(jar http.CookieJar) RequestIDer {
return func(req *http.Request) string {
for _, c := range jar.Cookies(req.URL) {
if c.Name == RETSSessionID {
return c.Value
}
}
return ""
}
}
示例4: getSessionId
func getSessionId(jar http.CookieJar) string {
key := "phpbb2mysql_sid"
url, _ := url.Parse("egal")
for _, cookie := range jar.Cookies(url) {
if cookie.Name == key {
return cookie.Value
}
}
panic("no sessionid found")
}
示例5: SetCookie
// SetCookie creates a cookie in jar which is suitable for performing agent
// logins to u.
//
// If using SetUpAuth, it should not be necessary to use
// this function.
func SetCookie(jar http.CookieJar, u *url.URL, username string, pk *bakery.PublicKey) {
al := agentLogin{
Username: username,
PublicKey: pk,
}
b, err := json.Marshal(al)
if err != nil {
// This shouldn't happen as the agentLogin type has to be marshalable.
panic(errgo.Notef(err, "cannot marshal cookie"))
}
v := base64.StdEncoding.EncodeToString(b)
jar.SetCookies(u, []*http.Cookie{{
Name: cookieName,
Value: v,
}})
}
示例6: cookie_process
func cookie_process(cookiejar http.CookieJar, surl string, cookiedata string) {
if cookiedata == "" {
return
}
cookiedata = "HTTP/1.0 200 OK\r\n" + cookiedata + "\r\n\r\n"
req, err := http.NewRequest("GET", surl, nil)
if err != nil {
fmt.Println(err)
return
}
res, err := http.ReadResponse(bufio.NewReader(strings.NewReader(cookiedata)), req)
if err != nil {
fmt.Println(err)
return
}
cookies := res.Cookies()
turl, err := url.Parse(surl)
cookiejar.SetCookies(turl, cookies)
}
示例7: Do
// Start a request, and get the response.
//
// Usually we just need the Get and Post method.
func (this *HttpClient) Do(method string, url string, headers map[string]string,
body io.Reader) (*Response, error) {
options := mergeOptions(defaultOptions, this.Options, this.oneTimeOptions)
headers = mergeHeaders(this.Headers, this.oneTimeHeaders, headers)
cookies := this.oneTimeCookies
var transport http.RoundTripper
var jar http.CookieJar
var err error
// transport
if this.transport == nil || !this.reuseTransport {
transport, err = prepareTransport(options)
if err != nil {
this.reset()
return nil, err
}
if this.reuseTransport {
this.transport = transport
}
} else {
transport = this.transport
}
// jar
if this.jar == nil || !this.reuseJar {
jar, err = prepareJar(options)
if err != nil {
this.reset()
return nil, err
}
if this.reuseJar {
this.jar = jar
}
} else {
jar = this.jar
}
// release lock
this.reset()
redirect, err := prepareRedirect(options)
if err != nil {
return nil, err
}
c := &http.Client{
Transport: transport,
CheckRedirect: redirect,
Jar: jar,
}
req, err := prepareRequest(method, url, headers, body, options)
if err != nil {
return nil, err
}
if jar != nil {
jar.SetCookies(req.URL, cookies)
} else {
for _, cookie := range cookies {
req.AddCookie(cookie)
}
}
res, err := c.Do(req)
return &Response{res}, err
}
示例8: MacaroonsForURL
// MacaroonsForURL returns any macaroons associated with the
// given URL in the given cookie jar.
func MacaroonsForURL(jar http.CookieJar, u *url.URL) []macaroon.Slice {
return cookiesToMacaroons(jar.Cookies(u))
}