Home >WeChat Applet >Mini Program Development >How to use cookiejar to remember account in small program development

How to use cookiejar to remember account in small program development

Y2J
Y2JOriginal
2017-04-27 13:06:373104browse

The browser is stateless, the browser only has cookies, and the session is also a cookie on the client side. If you want the robot to remember your account/password for automatic login, the problem becomes letting it remember cookies.

Remember Cookie is extremely simple in golang.

Recently we were writing an operation robot, and one of our brothers realized the automatic login of the WeChat account. After briefly looking through the source code, we found that it was implemented using cookiejar.

cookiejar:The net/http/cookiejar package provides a CookieJar implementation.

cookiejar is extremely simple in golang source code:

type CookieJar interface {
//设置cookie        SetCookies(u *url.URL, cookies []*Cookie)
//取cookie        Cookies(u *url.URL) []*Cookie}
核心代码:
wechat.BaseURL = cached[`baseURL`].(string)
wechat.BaseRequest = cached[`baseRequest`].(*BaseRequest)
cookies := cached[`cookies`].([]*http.Cookie)
u, ue := url.Parse(wechat.BaseURL)
if ue != nil {
return ue
}

wechat.Client.Jar.SetCookies(u , cookies)

where cached is the json sequence data read from the hard disk, cached[`cookies`] is the cache written after previous login. And wechat.Client is a standard http.Client:

type WeChat struct {
Client      *http.Client
}

Reuse the same http.Client within a program cycle, or write the cookie to the hard disk or database, and reuse it the next time you restart. In this way, automatic login of the account is achieved.

The above is the detailed content of How to use cookiejar to remember account in small program development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn