The Go language HTTP request case list.

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

Go language request, small case list get with post:

Package Main Import ("FMT""Net/http""Io/ioutil""Strings""OS""IO"Func Main () {HttpGet () Content:=httppostparmas () savafile (content)}func HttpGet () {resp,Err: =http.Get("Http://www.baidu.com")if Err!=nil {fmt. Println ("already initialized")} Defer resp. Body.close () body,Err: =ioutil. ReadAll (resp. Body)if Err!=nil {fmt. Println ("Get body from Ioutil")} FMT. Println (string(body)) Fmt. Println ("---------------------------------------------------------------------------------------------------------- -------------------------------")}func Httppostparmas () (contentstring) {client: =&http. client{} Rep,Err: =http. Newrequest ("POST","Http://www.baidu.com", strings. Newreader ("NAME=CJB"))if Err!=nil{FMT. Println ("----HttpRequest-------")} Rep. Header.Set("Content-type","application/x-www-form-urlencoded") Rep. Header.Set("Cookie","Name=anny") RESP,Err: =client. Do(rep) Defer resp. Body.close () body,Err: =ioutil. ReadAll (resp. Body)if Err!=nil{FMT. Println ("----httprequest----Body---")} FMT. Println (string(body)) Returnstring(body)} Func Savafile (contentstring) {var file *os. File varErr Errorvar filenamestring="./temp.html"FileErr=os. Create (filename)if Err!=nil{Panic (Err)} N,Err: =io. WriteString (file,content)if Err!=nil {Panic (Err)} file. Close () fmt. Printf ("Write%d bytes n", n) file. Sync ()}
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.