Use Golang to get your computer automatically logged into the "Internet management" system

Source: Internet
Author: User
Tags form post

Use Golang to get your computer to log on automatically

1. Origins

The big Celestial Network is generally only open to some people. Shots, the same measure also works on our education online. I can't think why a lot of leaders have this kind of bad taste (if they do, they can't wait to monitor their deeds). Every time we surf the Internet within our education network, we have to go through an "Internet management" system. Of course, the "real name registration" must be required. For an honest and evolved monkey like me, this is obviously redundant, and every time you turn it on, it's a bad thing. Then play the initiative and let it be automated.

2. Logic

The program logic is very simple, first get a Web page (sorry to choose the baidu.com), if not logged in, "Internet management" system will redirect to its login page, I only need to search the returned content contains the need to login information, if you need to log in, post an account information to it, This allows for automatic login.

3. Implement

Golang has pre-provisioned a lot of network access packages, access to HTTP content can use the functions in the Net/http package, the use is very simple. The code is as follows:

This procedure is used for Xinchang education Network automatic RecruitmentPackage MainImport("Flag""FMT""Io/ioutil""Net/http""Net/url""Strings") funcMain(){The following three sentences are used to process parameters, note flag. Parse () must be called.The flag packet is very useful and can automatically handle many details of the command line parameters.For example: Automatic processing of--help, it will output the information we set the user:= Flag.String("User","","Input your user name.") pwd:= Flag.String("PWD","","Input your password.") flag.Parse()If*user==""||*pwd==""{FMT.Println("Please input user name and password!")Return}Get a webpage, pay attention to turn off the responseI used one of the Golang skills: defer, which executes the specified content before the function is finished resErr:= http.Get("Http://www.baidu.com")If err! = Nil{FMT.Println("Network is error.")Return}defer Res. Body.Close()Use the functions in Ioutil to easily read the contents of the Web page get to. Although we can also read it ourselves, but which is so convenient ah. Data, _:= Ioutil.ReadAll(Res. Body)Find out if there is a "not signed in" string idx:= Strings.Index(String(Data),"<title> Online authentication System-Login </title>")If IDX!=-1{Using the Net/url package to manage post data, you can simply synthesize the string yourself for simple ASCII content.But with it, you can guarantee that there will be no errors. V:= URL. Values{}v.Set("Username",*user) v.Set("Password",*pwd) v.Set("PWD",*pwd) body:= Strings.Newreader(V.Encode())Coding the form dataCreating an HTTP Object client:=&http. Client{}Establishing an HTTP Request object req, _:= http.Newrequest("POST","http://172.16.254.10/webAuth/", body)This must be added, without the value of the form post does not pass req. Header.Set("Content-type","Application/x-www-form-urlencoded; Param=value ") RESP, _:= Client.Do(req)Send Defer RESP. Body.Close()Close the resp. Bodydata, _:= Ioutil.ReadAll(RESP. Body) IDX:= Strings.Index(String(Data),"<font color=\" red\ "> Login successful, you can surf the internet </font>"if idx != -1 {fmt println ( "login Success!" }//fmt. Println (String (data), err) } else {fmt. Println ( "you are alread login!" }}    /span>                

All the code has been commented, I believe you can easily read.

4. Use

I usually use the Windows platform, here I talk about the use of Windows platform. First use Golang to generate EXE file, then left click on the exe file "Create Shortcut", edit this shortcut, right click on the shortcut, click Properties, in the "target" with our login account and password, may be as follows (note your executable file path):

F:\gofile\src\edu-login\edu-login.exe -user=xxxx -pwd=xxx

Then drag the shortcut to the Windows Startup folder. WinXP and Windows7 slightly different, you can search the solution. Because it's too easy, there's no introduction here.

5. Summary

At this point, our tall automatic login software is done. Now everyone can enjoy the convenience of the program ape. Using Golang can not only realize automatic login, use it to write a web crawler is also easy, and with Golang unique language features, crawler performance will be excellent.

http://www.misitang.com/index.php/archives/117/

Use Golang to get your computer automatically logged into the "Internet management" system

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.