Import ( "Net/http" "Net/url")//Send a simple GET request func Getrequest { //contact Use make (map[string]string) Querydata: = Make (map[string]string) //Create space, querydata["params"] = C.queryparam ("params") u, _: = URL. Parse ("Http://baidu.com/api/member/getUserSafeDevic") Q: = U.query () q.set ("params", querydata["params"]) u.rawquery = Q.encode () resp, _: = http. Get (U.string ()) //Open a GET request note that the parameters in get are String result, _: = Ioutil. ReadAll (resp. Body) //The body data returned by the interface to result resp. Body.close () //Close request FMT. Printf ("%s", resp) //Print Results}
//via HTTP. Client sends a POST requestfunc postusersafedevice {q:=URL. values{} q.set ("mac", C.queryparam ("mac")) Q.set ("IMEI", C.queryparam ("IMEI") ) Body:=Ioutil. Nopcloser (Strings. Newreader (Q.encode ())) Client:= &http. client{} req, _:= http. Newrequest ("POST","Http://baidu.com/postDevice", body) req. Header.set ("Content-type","application/x-www-form-urlencoded; Param=value") Resp, _:=client. Do (req) defer resp. Body.close () result, _:=Ioutil. ReadAll (resp. Body) fmt. Printf ("%s", result)}
Golang (Second article initiates an HTTP request)