2012-10-06 16 views
17

का उपयोग करें मैं वेबसाइट पर लॉग इन करने के लिए गो का उपयोग करने और बाद में उपयोग के लिए कुकीज़ स्टोर करने का प्रयास कर रहा हूं।HTTP पोस्ट पर जाएं और कुकीज़

क्या आप एक फॉर्म पोस्ट करने, कुकीज़ संग्रहीत करने और कुकीज़ का उपयोग करके किसी अन्य पृष्ठ तक पहुंचने के लिए उदाहरण कोड दे सकते हैं?

मैं मैं कुकी संग्रहीत करने के लिए एक ग्राहक बनाने के लिए, का अध्ययन http://gotour.golang.org/src/pkg/net/http/client.go

package main 

import ("net/http" 
     "log" 
     "net/url" 
     ) 

func Login(user, password string) string { 
     postUrl := "http://www.pge.com/eum/login" 

     // Set up Login 
     values := make(url.Values) 
     values.Set("user", user) 
     values.Set("password", password) 

     // Submit form 
     resp, err := http.PostForm(postUrl, values) 
     if err != nil { 
       log.Fatal(err) 
     } 
     defer resp.Body.Close() 

     // How do I store cookies? 
     return "Hello" 
} 

func ViewBill(url string, cookies) string { 

//What do I put here? 

} 
+1

दुर्भाग्य से एक मानक कुकी 'Jar' implemntation Go1 में यह नहीं कर सकता था, लेकिन ऐसा लगता है कि यह भविष्य वर्धन के लिए योजना बनाई है दिखता है: https://codereview.appspot.com/5544082/ –

+0

http का उपयोग कर को देखो: //www.gorillatoolkit.org/pkg/sessions – elithrar

उत्तर

39

जाओ 1.1 एक कुकी जार कार्यान्वयन net/http/cookiejar की शुरुआत की।

import (
    "net/http" 
    "net/http/cookiejar" 
) 

cookieJar, _ := cookiejar.New(nil) 

client := &http.Client{ 
    Jar: cookieJar, 
} 
14

सबसे पहले आप http.CookieJar इंटरफ़ेस को लागू करने की आवश्यकता होगी द्वारा की जरूरत हो सकती है। इसके बाद आप इसे अपने द्वारा बनाए गए क्लाइंट में पास कर सकते हैं और इसका उपयोग ग्राहक के साथ किए गए अनुरोधों के लिए किया जाएगा। एक बुनियादी उदाहरण के रूप में:

package main 

import (
    "fmt" 
    "net/http" 
    "net/url" 
    "io/ioutil" 
    "sync" 
) 

type Jar struct { 
    lk  sync.Mutex 
    cookies map[string][]*http.Cookie 
} 

func NewJar() *Jar { 
    jar := new(Jar) 
    jar.cookies = make(map[string][]*http.Cookie) 
    return jar 
} 

// SetCookies handles the receipt of the cookies in a reply for the 
// given URL. It may or may not choose to save the cookies, depending 
// on the jar's policy and implementation. 
func (jar *Jar) SetCookies(u *url.URL, cookies []*http.Cookie) { 
    jar.lk.Lock() 
    jar.cookies[u.Host] = cookies 
    jar.lk.Unlock() 
} 

// Cookies returns the cookies to send in a request for the given URL. 
// It is up to the implementation to honor the standard cookie use 
// restrictions such as in RFC 6265. 
func (jar *Jar) Cookies(u *url.URL) []*http.Cookie { 
    return jar.cookies[u.Host] 
} 

func main() { 
    jar := NewJar() 
    client := http.Client{nil, nil, jar} 

    resp, _ := client.PostForm("http://www.somesite.com/login", url.Values{ 
     "email": {"myemail"}, 
     "password": {"mypass"}, 
    }) 
    resp.Body.Close() 

    resp, _ = client.Get("http://www.somesite.com/protected") 

    b, _ := ioutil.ReadAll(resp.Body) 
    resp.Body.Close() 

    fmt.Println(string(b)) 
} 
+0

आपको शायद कुकी जार के लिए होस्टनाम पर एक नक्शा का उपयोग करना चाहिए। एक सामान्य समाधान के लिए –

+0

, सहमत हुए। यह ब्राउज़र के काम के समान ही होगा, लेकिन अंत में यह कार्यान्वयन विवरणों पर निर्भर करता है। – dskinner

+0

"कुकीजार के कार्यान्वयन एकाधिक goroutines द्वारा समवर्ती उपयोग के लिए सुरक्षित होना चाहिए।" यह समेकन सुरक्षित नहीं है। इस तथ्य का जिक्र नहीं है कि यह आपकी कुकीज़ को अन्य होस्टों को भेजता है। मैं डाउनवोट करने जा रहा हूँ। –

5

गो के संस्करण 1.5 पर, हम कुकी के साथ पोस्ट अनुरोध करने के लिए http.NewRequest का उपयोग कर सकते हैं।

package main                        
import "fmt" 
import "net/http" 
import "io/ioutil" 
import "strings" 

func main() { 
    // Declare http client 
    client := &http.Client{} 

    // Declare post data 
    PostData := strings.NewReader("useId=5&age=12") 

    // Declare HTTP Method and Url 
    req, err := http.NewRequest("POST", "http://localhost/", PostData) 

    // Set cookie 
    req.Header.Set("Cookie", "name=xxxx; count=x") 
    resp, err := client.Do(req) 
    // Read response 
    data, err := ioutil.ReadAll(resp.Body) 

    // error handle 
    if err != nil { 
     fmt.Printf("error = %s \n", err); 
    } 

    // Print response 
    fmt.Printf("Response = %s", string(data)); 
}   
0

ऐसा करने का एक और तरीका। गो 1.8 में काम करता है।

expiration := time.Now().Add(5 * time.Minute) 
    cookie := http.Cookie{Name: "myCookie", Value: "Hello World", Expires: expiration} 
    http.SetCookie(w, &cookie) 
संबंधित मुद्दे