本文整理汇总了Golang中net/http.Response.Cookies方法的典型用法代码示例。如果您正苦于以下问题:Golang Response.Cookies方法的具体用法?Golang Response.Cookies怎么用?Golang Response.Cookies使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类net/http.Response
的用法示例。
在下文中一共展示了Response.Cookies方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: processResponse
func (self *session) processResponse(res *http.Response) (r *response, err error) {
ret := &response{
code: res.StatusCode,
}
if h, ok := res.Header["Location"]; ok {
ret.redir = h[0]
}
ret.body, err = ioutil.ReadAll(res.Body)
if err != nil {
return ret, err
}
ret.cookies = map[string]string{}
for _, cookie := range res.Cookies() {
ret.cookies[cookie.Name] = cookie.Value
if cookie.Name == "SESSION" {
if self.session == "" {
self.session = cookie.Value
}
}
}
return ret, nil
}
示例2: setupStickySession
func setupStickySession(responseWriter http.ResponseWriter, response *http.Response,
endpoint *route.Endpoint,
originalEndpointId string,
secureCookies bool,
path string) {
maxAge := 0
// did the endpoint change?
sticky := originalEndpointId != "" && originalEndpointId != endpoint.PrivateInstanceId
for _, v := range response.Cookies() {
if v.Name == StickyCookieKey {
sticky = true
if v.MaxAge < 0 {
maxAge = v.MaxAge
}
break
}
}
if sticky {
cookie := &http.Cookie{
Name: VcapCookieId,
Value: endpoint.PrivateInstanceId,
Path: path,
MaxAge: maxAge,
HttpOnly: true,
Secure: secureCookies,
}
http.SetCookie(responseWriter, cookie)
}
}
示例3: ParseResponse
func ParseResponse(resp *http.Response, captureContent bool) *Response {
if resp == nil {
return nil
}
statusText := resp.Status
if len(resp.Status) > 4 {
statusText = resp.Status[4:]
}
redirectURL := resp.Header.Get("Location")
harResponse := Response{
Status: resp.StatusCode,
StatusText: statusText,
HttpVersion: resp.Proto,
Cookies: parseCookies(resp.Cookies()),
Headers: parseStringArrMap(resp.Header),
RedirectUrl: redirectURL,
BodySize: resp.ContentLength,
HeadersSize: calcHeaderSize(resp.Header),
}
if captureContent {
parseContent(resp, &harResponse.Content)
}
return &harResponse
}
示例4: WriteRequest
func (codec *clientCodec) WriteRequest(request *rpc.Request, args interface{}) (err error) {
httpRequest, err := NewRequest(codec.url.String(), request.ServiceMethod, args)
if codec.cookies != nil {
for _, cookie := range codec.cookies.Cookies(codec.url) {
httpRequest.AddCookie(cookie)
}
}
if err != nil {
return err
}
var httpResponse *http.Response
httpResponse, err = codec.httpClient.Do(httpRequest)
if err != nil {
return err
}
if codec.cookies != nil {
codec.cookies.SetCookies(codec.url, httpResponse.Cookies())
}
codec.responses[request.Seq] = httpResponse
codec.ready <- request.Seq
return nil
}
示例5: setUserSession
func setUserSession(w http.ResponseWriter, r *http.Request, username string) {
handler.SetCookie(w, r, "_sess", username)
resp := http.Response{Header: w.Header()}
for _, v := range resp.Cookies() {
r.AddCookie(v)
}
}
示例6: updateAuth
//Couchdb returns updated AuthSession tokens
func (ca *CookieAuth) updateAuth(resp *http.Response) {
for _, cookie := range resp.Cookies() {
if cookie.Name == "AuthSession" {
ca.UpdatedAuthToken = cookie.Value
}
}
}
示例7: getSessionAndAppPort
func getSessionAndAppPort(url string, rPort uint16, c *C) (string, string, string) {
var client http.Client
var req *http.Request
var resp *http.Response
var err error
var port []byte
uri := fmt.Sprintf("http://%s:%d/sticky", url, rPort)
req, err = http.NewRequest("GET", uri, nil)
resp, err = client.Do(req)
c.Assert(err, IsNil)
port, err = ioutil.ReadAll(resp.Body)
var session string
var path string
for _, cookie := range resp.Cookies() {
if cookie.Name == "__VCAP_ID__" {
session = cookie.Value
path = cookie.Path
}
}
return session, string(port), path
}
示例8: ResponseToJso
func ResponseToJso(o *otto.Object, w *http.Response) {
o.Set("status", w.Status)
o.Set("header", w.Header)
o.Set("cookies", w.Cookies())
o.Set("statusCode", w.StatusCode)
o.Set("proto", w.Proto)
c, _ := ioutil.ReadAll(w.Body)
o.Set("body", string(c))
if w.TLS != nil {
o.Set("tlsServerName", w.TLS.ServerName)
o.Set("tlsNegotiatedProtocol", w.TLS.NegotiatedProtocol)
names := []string{}
ips := []string{}
for _, k := range w.TLS.PeerCertificates {
for _, n := range k.DNSNames {
names = append(names, n)
}
for _, n := range k.IPAddresses {
ips = append(ips, n.String())
}
}
o.Set("tlsDNSNames", names)
o.Set("tlsIPs", ips)
}
}
示例9: SetBasicInfo
func (info *Info) SetBasicInfo(url string, response *http.Response) {
info.Url = url
for key, values := range response.Header {
info.RawHeaders[key] = values
}
cookies := response.Cookies()
for _, cookie := range cookies {
info.Cookies[cookie.Name] = cookie
}
u, err := urlLib.Parse(url)
if err != nil {
panic(err)
}
info.Host = u.Host
ips, err := net.LookupHost(u.Host)
if err == nil {
info.Ip = ips
for _, ip := range info.Ip {
hosts, err := net.LookupAddr(ip)
if err == nil {
info.RealHost = hosts
}
}
}
}
示例10: downloadFile
//下载文件,并对字符编码做相应的处理
func (this *HttpDownLoader) downloadFile(p *page.Page, req *page.Request) (*page.Page, string) {
var err error
var httpResp *http.Response
var urlStr string
var method string
urlStr = req.GetUrl()
if len(urlStr) == 0 {
logs.GetFirstLogger().Error("url is empty")
p.SetStatus(true, "url is empty")
return p, ""
}
method = req.GetMethod()
if method == "POST" {
httpResp, err = http.Post(req.GetUrl(), "application/x-www-form-urlencoded", strings.NewReader(req.GetPostData()))
} else {
httpResp, err = http.Get(req.GetUrl())
}
if err != nil {
logs.GetFirstLogger().Error("http visit error :" + err.Error())
p.SetStatus(true, err.Error())
}
p.SetHeader(httpResp.Header)
p.SetCookies(httpResp.Cookies())
body, _ := ioutil.ReadAll(httpResp.Body)
bodyStr := string(body)
defer httpResp.Body.Close()
return p, bodyStr
}
示例11: getSessionAndAppPort
func getSessionAndAppPort(url string, rPort uint16) (*http.Cookie, *http.Cookie, string) {
var client http.Client
var req *http.Request
var resp *http.Response
var err error
var port []byte
uri := fmt.Sprintf("http://%s:%d/sticky", url, rPort)
req, err = http.NewRequest("GET", uri, nil)
Expect(err).ToNot(HaveOccurred())
resp, err = client.Do(req)
Expect(err).ToNot(HaveOccurred())
port, err = ioutil.ReadAll(resp.Body)
Expect(err).ToNot(HaveOccurred())
var sessionCookie, vcapCookie *http.Cookie
for _, cookie := range resp.Cookies() {
if cookie.Name == proxy.StickyCookieKey {
sessionCookie = cookie
} else if cookie.Name == proxy.VcapCookieId {
vcapCookie = cookie
}
}
return sessionCookie, vcapCookie, string(port)
}
示例12: generate
func generate(credentials string, url string, attempts int) (bool, []int) {
transport := &http.Transport{}
var response *http.Response
//log.Printf("hitting %s", url)
// first try real string without trailing bytes to test a win
req := request(url, credentials)
response, _ = transport.RoundTrip(req)
if response.StatusCode == 200 {
log.Printf("body %v", response.Body)
// return true, nil
}
cookies := response.Cookies()
response.Body.Close()
req = request(url, credentials+" ")
timing := make([]int, 0)
for _, i := range cookies {
req.AddCookie(i)
}
for true {
before := time.Now()
response, err := transport.RoundTrip(req)
duration := time.Since(before)
_ = duration
if err != nil {
log.Printf("Error %v", err)
continue
}
if response.StatusCode == 200 {
return true, nil
}
response.Body.Close()
headertime, _ := strconv.ParseFloat(response.Header.Get("X-Runtime"), 64)
_ = headertime
// log.Printf("X-Runtime = %v, %v, %v", response.Header.Get("X-Runtime"), headertime, int64(headertime*1000000))
timing = append(timing, int(duration.Nanoseconds()))
if len(timing) >= attempts {
break
}
//sleep(200)
}
return false, timing
}
示例13: getRespCookie
// Gets a cookie with the specified name from the Response
// Returns nil on not finding a suitable cookie
func getRespCookie(resp *http.Response, name string) *http.Cookie {
for _, c := range resp.Cookies() {
if c.Name == name {
return c
}
}
return nil
}
示例14: refresh
// refresh updates the cookie and expiration used to sign requests from a successful session
// creation API response.
func (s *cookieSigner) refresh(resp *http.Response) error {
if resp.StatusCode != 204 {
return fmt.Errorf("Authentication failed: %s", resp.Status)
}
s.cookies = resp.Cookies()
s.refreshAt = time.Now().Add(time.Duration(2) * time.Hour)
return nil
}
示例15: findResponseCookie
func findResponseCookie(response *http.Response, name string) string {
for _, cookie := range response.Cookies() {
if cookie.Name == name {
return cookie.Value
}
}
return ""
}