Golang Analog web Uploads

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

Recently, I have been studying the contents of Golang and I have written something today.

Learning is based on some articles on the web. But the features are brand new.

1) Implement Golang request get

2) Enable post login

3) implement web uploads (based on pp.sohu.com)

Code implementation:

Package Mainimport ("base" "bytes" "FMT" "io" "io/ioutil" "Log" "Mime/multipart" "Net/http" "Net/http/cookiejar" "Net/url" "OS" "RegExp" "strings") var gcurcookies []*http. Cookievar Gcurcookiejar *cookiejar. Jarvar logger *log. Logger/*uploadbodyele struct--ad36e31ab0e27a44e9038057dc8d0a39aceb12b26f27fd6d375148e6b81acontent-disposition: Form-data; Name= "Filename" Test.jpg*/type uploadbodyele struct {boundary Stringkey stringvalue string}/*use these info to cons  Truct the whole upload body string*/type uploadbodyinfo struct {elemap map[string]stringboundary stringfilecontent String}func Initall () {gcurcookies = Nilgcurcookiejar, _ = Cookiejar. New (nil) Logg: = base. Newlogger ("Log", "Log.") Logger = Logg. Log ()}func readuploadfile (filename string) string {buf: = new (bytes. Buffer) W: = multipart. Newwriter (BUF) fw, Err: = W.createformfile ("file", filename) if err! = Nil {fmt. Println ("C")}fd, err: = OS. Open (filename) defer FD. Close () if err! = Nil {fmt. Println ("D")}_, err = io. Copy (FW, FD) if err! = NIl {fmt. Println ("E")}w.close () return BUF. String ()}func constructuploadbodyelestr (ele *uploadbodyele) string {return FMT. Sprintf ("--%s\ncontent-disposition:form-data; Name=\ "%s\" \n\n%s\n ", Ele.boundary, Ele.key, Ele.value)}func constructuploadbodystr (info *uploadbodyinfo) string {var Str stringfor _key, _value: = Range Info.elemap {uploadbodyele: = &uploadbodyele{boundary:info.boundary,key: _ke Y,value: _value,}str + = Constructuploadbodyelestr (uploadbodyele)}str + = Fmt. Sprintf ("%s\n", info.filecontent) return Str}func constructuploadreq (strURL string, Postdict map[string]string, FileName string) *http. Request{var httpreq *http. request/*------------------------Content----------------------------*/file_content: = readuploadfile (filename) Info: = &uploadbodyinfo{elemap:postdict,boundary:file_content[2:strings. Index (file_content, "\ n")],filecontent:file_content,}postdatastr: = Constructuploadbodystr (info) Postbytesreader: = bytes. Newreader ([]byte (POSTDATASTR)) HttpreQ, _ = http. Newrequest ("POST", strURL, Postbytesreader)/*------------------------Head----------------------------*/ HTTPREQ.HEADER.ADD ("Content-type", FMT. Sprintf ("Multipart/form-data; boundary=%s ", info.boundary)) return Httpreq}func geturlresphtml (httpClient *http. Client, strURL string, postdict map[string]string, filename string), string {var httpreq *http. Requestif Nil = = postdict {logger. Println ("---------------is GET------------------") httpreq, _ = http. Newrequest ("GET", strURL, Nil) httpReq.Header.Set ("Accept", "Application/json, Text/javascript, */*; q=0.01 ")} else {if filename = =" "{logger. Println ("---------------is POST------------------") postvalues: = URL. Values{}for Postkey, Postvalue: = Range postdict {postvalues.set (Postkey, postvalue)}postdatastr: = PostValues.Encode () Postbytesreader: = bytes. Newreader ([]byte (POSTDATASTR)) httpreq, _ = http. Newrequest ("POST", strURL, Postbytesreader) httpReq.Header.Add ("Content-type", "application/x-www-form-urlencoded" )}else {logger. Println ("---------------is POST Upload------------------") Httpreq = Constructuploadreq (strURL, postdict, filename)} HTTPRESP, err: = Httpclient.do (httpreq) if err! = Nil {logger. Println ("HTTP Get strurl=%s response error=%s", strURL, err.) Error ())}//logger. Println ("Httpresp.status:%s", httpresp.status) defer httpResp.Body.Close () Body, Errreadall: = Ioutil. ReadAll (httpresp.body) if errreadall! = Nil {logger. PRINTLN ("Get response for strurl=%s got Error=%s\n", strURL, Errreadall.error ())}gcurcookies = Gcurcookiejar.cookies ( Httpreq.url) Resphtml: = string (body) return Resphtml}func printcurcookies () {var cookienum int = len (gcurcookies) logger. Println ("cookienum=%d", Cookienum) for I: = 0; i < Cookienum; i++ {var Curck *http. Cookie = Gcurcookies[i]logger. PRINTLN ("------Cookie [%d]------", i) logger. Println ("name\t\t=%s", curck.name) logger. Println ("value\t=%s", curck.value) logger. Println ("path\t\t=%s", Curck.path) logger. Println ("domain\t=%s", Curck.domain) logger. Println ("expires\t=%s", curck.expires) logger. Println ("rawexpires\t=%s", curck.rawexpires) logger. Println ("maxage\t=%d", curck.maxage) logger. Println ("Secure\t=%t", curck.secure) logger. Println ("Httponly\t=%t", curck.httponly) logger. Println ("raw\t\t=%s", Curck.raw) logger. Println ("unparsed\t=%s", curck.unparsed)}}func main () {Initall () httpClient: = &http. Client{jar:gcurcookiejar,}//------------------------------------------- Page Split Line------------------------------------------postdict: = map[string]string{}postdict["AppID"] = "9998" postdict[" Persistentcookie "] =" 0 "postdict[" b "] =" 7 "postdict[" W "] =" 1440 "postdict[" pwdtype "] =" 1 "postdict[" V "] =" postdict[" Isslogin "] =" 1 "postdict[" ru "] =" Https://passport.sohu.com/user/tologin "postdict[" domain "] =" sohu.com "postdict[" Loginsuccesscallfunction "] =" Postloginsuccesscall "postdict[" loginfailcallfunction "] =" postloginfailcall "// strUserName: = ""//strpassword: = ""//logger. PRINTLN ("Plese input:")//logger. Println ("Username:")//_, err1: = Fmt. ScaNLN (&strusername)//if nil = = err1 {//logger. Println ("strusername=%s", strUserName)//}//logger. Println ("Password:")//_, err2: = Fmt. Scanln (&strpassword)//if nil = = err2 {//logger. Println ("strpassword=%s", strpassword)//}//postdict["userid"] = strusername//postdict["password"] = strpasswordpostdict["userid"] = "pengnenghui04@sohu.com" postdict["password"] = "1775520214" logger. Println ("postdict=%s", postdict) Mainloginurl: = "Https://passport.sohu.com/user/login" loginresphtml: = Geturlresphtml (HttpClient, Mainloginurl, Postdict, "") logger. Println ("loginresphtml=%s", loginresphtml) printcurcookies ()//------------------------------------------- Page Split Line------------------------------------------tourl_1: = "Http://pp.sohu.com/folders" Toresphtml_1: = Geturlresphtml (httpClient, Tourl_1, Nil, "") logger. Println ("toresphtml=%s", Toresphtml_1) Printcurcookies () m: = RegExp. Mustcompile ("\" showid\ ": \". *\ ", \" name\ ": \". *\ ", \" userid\ "") List: = M.findstringsubmatch (toresphtml_1) logger. PRINTLN (list) varTourl_2 stringfor _, str: = Range list {start: = Len ("\" showid\ ": \" ") Start_: = Len (" \ ", \" name\ ": \") End: = Strings. Index (str, "\", \ "name\": \ "") End_: = Strings. LastIndex (str, "\", \ "userid\" ")//logger. PRINTLN (start,start_,end,end_) logger. Println (Str[start:end]) logger. Println (Str[start_+end:end_]) tourl_2 = FMT. Sprintf ("http://pp.sohu.com/upload?folder_id=%s", Str[start:end])}//------------------------------------------- Page Split Line------------------------------------------logger. Println (tourl_2) postdict_2: = map[string]string{}postdict_2["Upload"] = "Submit Query" postdict_2["Filename"] = " Test.jpg "toresphtml_2: = geturlresphtml (HttpClient, tourl_2, postdict_2," test.jpg ") logger. Println ("toresphtml=%s", toresphtml_2) printcurcookies ()}


A GET request is simple, and a POST request can be fidder with a packet, constructed by map[string]string

Post upload upload is a bit more complicated, but it's also the construction of Fidder intercepted packets


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.