首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >转到HTTP Post并使用Cookie

转到HTTP Post并使用Cookie
EN

Stack Overflow用户
提问于 2012-10-06 12:40:56
回答 4查看 61.1K关注 0票数 36

我正在尝试使用Go登录一个网站,并存储cookies以备后用。

您能给出发布表单、存储cookies以及使用cookies访问其他页面的示例代码吗?

通过研究http://gotour.golang.org/src/pkg/net/http/client.go,我想我可能需要做一个客户端来存储这些cookie

代码语言:javascript
复制
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?

}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2013-10-16 00:44:16

Go 1.1引入了一个cookie jar实现net/http/cookiejar

代码语言:javascript
复制
import (
    "net/http"
    "net/http/cookiejar"
)

jar, err := cookiejar.New(nil)
if err != nil { // error handling }

client := &http.Client{
    Jar: jar,
}
票数 74
EN

Stack Overflow用户

发布于 2012-10-06 13:20:02

首先,您需要实现http.CookieJar接口。然后,您可以将其传递到您创建的客户端,它将用于向客户端发出请求。举个基本的例子:

代码语言:javascript
复制
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))
}
票数 19
EN

Stack Overflow用户

发布于 2016-03-11 01:45:41

在Go的1.5版本中,我们可以使用http.NewRequest通过cookie发出post请求。

代码语言:javascript
复制
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));
}           
票数 15
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12756782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档