Golang automatically login to HTTPS connection and parse the resulting JSON return value

Source: Internet
Author: User
Tags uppercase letter
This is a creation in Article, where the information may have evolved or changed.

        • The JSON data format returned by HTTPS is
        • Go source is as follows

The main demonstration of the Go language automatic login Htpps connection and the use of cookies, how to parse JSON data and other features

The JSON data format returned by HTTPS is

typestruct {    Id, List_type, Severity            int    string    list_type_name, Severity_name      string}typestruct {    int    Sort_by, Sort_order                                string    List                                               []item}

Note that the struct must be a member of the beginning of the uppercase letter before it is JSON. Unmarshal () processing to, the corresponding data sample:

{    "Total_count" :366,    "Total_page" :1,    "Expect_page" :1,    "Num_per_page" :10000000,    "sort_by" :"id",    "Sort_order" :"desc",    "List" :[{ "ID" :3263, "List_type" :2, "list_type_string" :"File", "name" :"4cda46b428405e8a155a3aa32e4f25c8ea09b707", "severity" :3, "Expiration" :"11\/14\/2014 04:51:06", "List_type_name" :"File", "Severity_name" : "High" }, { "ID" :3262, "List_type" :2, "list_type_string" :"File", "name" :"Ecc3f6d2cb227981c12038d620336617c8ec0b64", "severity" :3, "Expiration" :"11\/12\/2014 10:02:21", "List_type_name" :"File", "Severity_name" : "High" } ]}

Go source is as follows

//HTTPS project Main.go PackageMainImport("Crypto/tls"    "Encoding/json"    "FMT"    "Io/ioutil"    "Net/http"    "Net/http/cookiejar"    "Net/url")typeItemstruct{Id, List_type, SeverityintList_type_string, Name, expirationstringList_type_name, Severity_namestring}typeResdatastruct{total_count, total_page, Expect_page, Num_per_pageintSort_by, Sort_orderstringList []item}funcCheck (err error) {ifErr! =Nil{Panic(ERR)}}funcMain () {//Login URL, which provides the URL of the account loginLoginurl: ="https://10.64.71.8/cgi-bin/logon.cgi"Logindata: = URL. values{"Usrname": {"Admin"},"passwd": {"Trend#11"},"Iscookieenable": {"1"}}//destination URL, which is the URL that needs to be accessedTargetUrl: ="https://10.64.71.8/php/blacklist_whitelist_query.php"Targetdata: = URL. values{"Action": {"Query"},"Black_or_white": {"Sandbox"},"Expect_page": {"1"},"Num_per_page": {"10000000"},"List_type": {" the"},"keyword": {""},"Sort_by": {"id"},"Sort_order": {"desc"}}varResp *http. ResponsevarErr errorvardata []byteTR: = &http. transport{tlsclientconfig: &tls. Config{insecureskipverify:true}, Disablecompression:true,} client: = &http. CLIENT{TRANSPORT:TR}//Enable cookiesClient. Jar, _ = Cookiejar. New (Nil) RESP, err = client. Postform (loginurl, logindata) Check (err) resp, err = client. Postform (TargetUrl, targetdata) Check (ERR)ifData, err = Ioutil. ReadAll (resp. Body); Err = =Nil{FMT. Printf ("%s\n", data)}//Parse the returned JSON data    varMessage Resdata err = json. Unmarshal (data, &message) Check (err) fmt. Printf ("%+v\n", message) FMT. Printf ("%v\t%v\n", message. Total_count, message. List[0]. Name)}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.