Skip to content

Instantly share code, notes, and snippets.

@mrtdeh
Forked from varver/cookie_jar_golang.go
Last active December 23, 2017 20:39

Revisions

  1. mrtdeh revised this gist Dec 23, 2017. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions cookie_jar_golang.go
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ http://website.com/upser_profile_page .
    4) Now get html of this whole page and print it in log as a string .
    */
    ```


    package main

    @@ -52,4 +52,3 @@ func main() {
    }
    log.Println(string(data)) // print whole html of user profile data
    }
    ```
  2. mrtdeh renamed this gist Dec 23, 2017. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. mrtdeh revised this gist Dec 23, 2017. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion cookie_jar_golang
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ http://website.com/upser_profile_page .
    4) Now get html of this whole page and print it in log as a string .

    */

    ```

    package main

    @@ -52,3 +52,4 @@ func main() {
    }
    log.Println(string(data)) // print whole html of user profile data
    }
    ```
  4. @varver varver created this gist Aug 27, 2014.
    54 changes: 54 additions & 0 deletions cookie_jar_golang
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,54 @@
    /*
    This code Do the following stuff :
    1) login to a website called : website.com by submitting password and username on the page with url :- http://website.com/login
    2) Now after login using the cookies stored by this webiste access user profile page
    3) Now using same client which stored the required cookies make another post request to user profile page present at page :-
    http://website.com/upser_profile_page .
    4) Now get html of this whole page and print it in log as a string .

    */


    package main

    import (
    "code.google.com/p/go.net/publicsuffix"
    "io/ioutil"
    "log"
    "net/http"
    "net/http/cookiejar"
    "net/url"
    )

    func main() {
    options := cookiejar.Options{
    PublicSuffixList: publicsuffix.List,
    }
    jar, err := cookiejar.New(&options)
    if err != nil {
    log.Fatal(err)
    }
    client := http.Client{Jar: jar}
    resp, err := client.PostForm("http://website.com/login", url.Values{
    "password": {"loginpassword"},
    "username" : {"testuser"},
    })
    if err != nil {
    log.Fatal(err)
    }


    resp, err = client.PostForm("http://website.com/upser_profile_page", url.Values{
    "userid": {"2"},
    })
    if err != nil {
    log.Fatal(err)
    }

    data, err := ioutil.ReadAll(resp.Body)
    resp.Body.Close()
    if err != nil {
    log.Fatal(err)
    }
    log.Println(string(data)) // print whole html of user profile data
    }