{"id":4144,"date":"2018-10-28T08:28:48","date_gmt":"2018-10-28T00:28:48","guid":{"rendered":"https:\/\/ixyzero.com\/blog\/?p=4144"},"modified":"2018-10-28T08:28:48","modified_gmt":"2018-10-28T00:28:48","slug":"go%e8%af%ad%e8%a8%80%e5%ad%a6%e4%b9%a08-%e5%a6%82%e4%bd%95%e5%8f%91%e9%80%81http%e8%af%b7%e6%b1%82","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/4144.html","title":{"rendered":"Go\u8bed\u8a00\u5b66\u4e60#8-\u5982\u4f55\u53d1\u9001HTTP\u8bf7\u6c42"},"content":{"rendered":"<p>=Start=<\/p>\n<h4 id=\"id-\u6a21\u677f-\u7f18\u7531\uff1a\">\u7f18\u7531\uff1a<\/h4>\n<p>\u5b66\u4e60\u548c\u6574\u7406\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528Go \u8bed\u8a00\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u65b9\u4fbf\u4ee5\u540e\u4f7f\u7528\u548c\u53c2\u8003\u3002<\/p>\n<h4 id=\"id-\u6a21\u677f-\u6b63\u6587\uff1a\">\u6b63\u6587\uff1a<\/h4>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u89e3\u7b54\uff1a\">\u53c2\u8003\u89e3\u7b54\uff1a<\/h5>\n<p>\u9996\u5148\u770b\u4e00\u4e0b net\/http \u5305\u4e2d\u548cGET\/POST\u51fd\u6570\u76f8\u5173\u7684\u65b9\u6cd5\u548c\u7ed3\u6784\u4f53\u8bf4\u660e\uff1a<\/p>\n<pre class=\"lang:default decode:true \">func Get(url string) (resp *Response, err error)\r\n\r\nfunc Post(url, contentType string, body io.Reader) (resp *Response, err error)\r\n\r\nfunc PostForm(url string, data url.Values) (resp *Response, err error)\r\n\r\n\/\/ \u5bf9\u4e8ePOST\u8bf7\u6c42\u7684\u5185\u5bb9\u5b57\u6bb5\/\u53c2\u6570\uff0c\u9700\u8981\u662f io.Reader \u7c7b\u578b\uff0c\u800c\u5e38\u89c4\u7684\u51e0\u79cd\u65b9\u5f0f\u6709\uff1a\r\nstrings.NewReader(\"name=hi\")\r\nurl.Values{\"key\": {\"Value\"}, \"id\": {\"123\"}})\r\nbytes.NewBuffer([]byte(`{\"title\":\"a breakfast\"}`))\r\n\/\/ \u662f\u56e0\u4e3a\u5b83\u4eec\u5747\u5b9e\u73b0\u4e86 io.Reader \u63a5\u53e3\uff0c\u6240\u4ee5\u53ef\u4ee5\u4f7f\u7528\r\n\r\nfunc NewRequest(method, url string, body io.Reader) (*Request, error)\r\n\r\nfunc (c *Client) Do(req *Request) (*Response, error)\r\n\/\/ To make a request with custom headers, use NewRequest and Client.Do.\r\n\/\/ \u5982\u679c\u4f60\u60f3\u81ea\u5b9a\u4e49header\u5b57\u6bb5\u7684\u8bdd\uff0c\u8bf7\u4f7f\u7528 NewRequest \u51fd\u6570\u7ed3\u5408 Client.Do \u65b9\u6cd5\u3002\r\n`\r\n<\/pre>\n<p>\u7136\u540e\u518d\u7ed3\u5408\u51e0\u4e2a\u53ef\u5b9e\u9645\u8fd0\u884c\u7684\u4f8b\u5b50\u65b9\u4fbf\u7406\u89e3\u548c\u9a8c\u8bc1\uff1a<\/p>\n<pre class=\"lang:default decode:true\">package main\r\n\r\nimport (\r\n    \"bytes\"\r\n    \"io\/ioutil\"\r\n    \"log\"\r\n    \"net\/http\"\r\n    \"net\/url\"\r\n    \"strings\"\r\n)\r\n\r\nvar (\r\n    url_str  = \"http:\/\/myexternalip.com\/raw\"\r\n    url_post = \"https:\/\/httpbin.org\/post\"\r\n)\r\n\r\n\/\/ \u5bf9\u6307\u5b9aURL\u53d1\u8d77GET\u8bf7\u6c42\uff0c\u5e76\u4ee5string\u683c\u5f0f\u8fd4\u56deresponse\r\nfunc httpGet(url string) string {\r\n    resp, err := http.Get(url)\r\n    if err != nil {\r\n        \/\/ handle error\r\n        log.Printf(\"http.Get(%s) Error:[%v]\\n\", url, err)\r\n    }\r\n    defer resp.Body.Close() \/\/ \u4f7f\u7528defer\u51fd\u6570\u8fdb\u884c\u54cd\u5e94\u4f53\u7684\u81ea\u52a8close\r\n\r\n    body, err := ioutil.ReadAll(resp.Body) \/\/ \u8fd4\u56de\u7684body\u662f []byte \u7c7b\u578b\r\n    if err != nil {\r\n        \/\/ handle error\r\n        log.Printf(\"http.Get(%s) Error:[%v]\\n\", url, err)\r\n    }\r\n\r\n    return string(body) \/\/ \u76f4\u63a5\u7528 string \u65b9\u6cd5\u5c06 []byte \u7c7b\u578b\u8f6c\u6362\u6210 string \u7c7b\u578b\u5b57\u7b26\u4e32\r\n}\r\n\r\n\/\/ \u5bf9\u6307\u5b9aURL\u53d1\u8d77POST\u8bf7\u6c42\uff08\u7528\u7f16\u7801\u597d\u7684\u5b57\u7b26\u4e32\u683c\u5f0f\u6a21\u62df\u8868\u5355\u63d0\u4ea4\uff09\uff0c\u5e76\u4ee5string\u683c\u5f0f\u8fd4\u56deresponse\r\nfunc httpPost(url string) string {\r\n    resp, err := http.Post(url,\r\n        \"application\/x-www-form-urlencoded\",\r\n        strings.NewReader(\"name=hi&amp;age=20\"))\r\n    if err != nil {\r\n        log.Printf(\"http.Post(%s) Error:[%v]\\n\", url, err)\r\n    }\r\n    defer resp.Body.Close()\r\n\r\n    body, err := ioutil.ReadAll(resp.Body)\r\n    if err != nil {\r\n        \/\/ handle error\r\n        log.Printf(\"http.Post(%s) Error:[%v]\\n\", url, err)\r\n    }\r\n\r\n    return string(body)\r\n}\r\n\r\n\/\/ \u5bf9\u6307\u5b9aURL\u53d1\u8d77POST\u8bf7\u6c42\uff08\u7528\u7c7b\u4f3ckv\u7684\u5f62\u5f0f\u586b\u5145\u8868\u5355\u63d0\u4ea4\u7684\u5185\u5bb9\uff09\uff0c\u5e76\u4ee5string\u683c\u5f0f\u8fd4\u56deresponse\r\nfunc httpPostForm(url_str string) string {\r\n    v := url.Values{}\r\n    v.Set(\"username\", \"xxxx\")\r\n    v.Set(\"password\", \"xxxx\")\r\n\r\n    resp, err := http.PostForm(url_str,\r\n        v)\r\n    \/\/ url.Values{\"key\": {\"Value\"}, \"id\": {\"123\"}})\r\n    if err != nil {\r\n        \/\/ handle error\r\n        log.Printf(\"http.PostForm(%s) Error:[%v]\\n\", url_str, err)\r\n    }\r\n    defer resp.Body.Close()\r\n\r\n    body, err := ioutil.ReadAll(resp.Body)\r\n    if err != nil {\r\n        \/\/ handle error\r\n        log.Printf(\"http.PostForm(%s) Error:[%v]\\n\", url_str, err)\r\n    }\r\n\r\n    return string(body)\r\n}\r\n\r\n\/\/ \u7528http.NewRequest+client.Do\u7684\u65b9\u5f0f\u53d1\u8d77\u53ef\u4ee5\u81ea\u5b9a\u4e49header\u7684POST\u8bf7\u6c42\uff08\u8868\u5355\u63d0\u4ea4\u683c\u5f0f\uff09\r\nfunc httpDo(url string) string {\r\n    client := &amp;http.Client{}\r\n\r\n    req, err := http.NewRequest(\"POST\", url, strings.NewReader(\"name=hi\"))\r\n    if err != nil {\r\n        \/\/ handle error\r\n        log.Printf(\"httpDo(%s) Error:[%v]\\n\", url, err)\r\n    }\r\n    req.Header.Set(\"Content-Type\", \"application\/x-www-form-urlencoded\")\r\n    req.Header.Set(\"Cookie\", \"name=anyone\")\r\n\r\n    resp, err := client.Do(req)\r\n    defer resp.Body.Close()\r\n\r\n    body, err := ioutil.ReadAll(resp.Body)\r\n    if err != nil {\r\n        \/\/ handle error\r\n        log.Printf(\"httpDo(%s) Error:[%v]\\n\", url, err)\r\n    }\r\n\r\n    return string(body)\r\n}\r\n\r\n\/\/ \u7528http.NewRequest+client.Do\u7684\u65b9\u5f0f\u53d1\u8d77\u53ef\u4ee5\u81ea\u5b9a\u4e49header\u7684POST\u8bf7\u6c42\uff08json\u683c\u5f0f\uff09\r\nfunc httpDoJson(url string) string {\r\n    client := &amp;http.Client{}\r\n\r\n    jsonStr := []byte(`{\"title\":\"a breakfast\"}`)\r\n    req, err := http.NewRequest(\"POST\", url, bytes.NewBuffer(jsonStr))\r\n    if err != nil {\r\n        \/\/ handle error\r\n        log.Printf(\"httpDo(%s) Error:[%v]\\n\", url, err)\r\n    }\r\n    req.Header.Set(\"Content-Type\", \"application\/json\")\r\n    req.Header.Set(\"Cookie\", \"id=anyone\")\r\n    req.Header.Set(\"User-Agent\", \"iPhone X\")\r\n\r\n    resp, err := client.Do(req)\r\n    defer resp.Body.Close()\r\n\r\n    body, err := ioutil.ReadAll(resp.Body)\r\n    if err != nil {\r\n        \/\/ handle error\r\n        log.Printf(\"httpDo(%s) Error:[%v]\\n\", url, err)\r\n    }\r\n\r\n    return string(body)\r\n}\r\n\r\nfunc main() {\r\n    body := httpGet(url_str)\r\n    log.Printf(\"httpGet(%s): '%s'\\n\\n\", url_str, strings.Replace(body, \"\\n\", \"\", -1))\r\n\r\n    body = httpPost(url_post)\r\n    log.Printf(\"httpPost(%s): '%s'\\n\\n\", url_post, body)\r\n\r\n    body = httpPostForm(url_post)\r\n    log.Printf(\"httpPostForm(%s): '%s'\\n\\n\", url_post, body)\r\n\r\n    body = httpDo(url_post)\r\n    log.Printf(\"httpDo(%s): '%s'\\n\\n\", url_post, body)\r\n\r\n    body = httpDoJson(url_post)\r\n    log.Printf(\"httpDoJson(%s): '%s'\\n\\n\", url_post, body)\r\n}<\/pre>\n<p>&amp;<\/p>\n<pre class=\"lang:default decode:true \">2018\/10\/26 23:59:25 httpDoJson(https:\/\/httpbin.org\/post): '{\r\n  \"args\": {},\r\n  \"data\": \"{\\\"title\\\":\\\"a breakfast\\\"}\",\r\n  \"files\": {},\r\n  \"form\": {},\r\n  \"headers\": {\r\n    \"Accept-Encoding\": \"gzip\",\r\n    \"Connection\": \"close\",\r\n    \"Content-Length\": \"23\",\r\n    \"Content-Type\": \"application\/json\",\r\n    \"Cookie\": \"id=anyone\",\r\n    \"Host\": \"httpbin.org\",\r\n    \"User-Agent\": \"iPhone X\"\r\n  },\r\n  \"json\": {\r\n    \"title\": \"a breakfast\"\r\n  },\r\n  \"origin\": \"216.22.34.83\",\r\n  \"url\": \"https:\/\/httpbin.org\/post\"\r\n}\r\n'<\/pre>\n<p>&nbsp;<\/p>\n<p>tips: \u8fd9\u91cc\u501f\u52a9\u7f51\u7ad9\u00a0<a href=\"https:\/\/httpbin.org\/post\" target=\"_blank\" rel=\"noopener\">https:\/\/httpbin.org\/post<\/a>\u00a0\uff08\u53ef\u4ee5\u628a\u6bcf\u6b21\u6211\u4eec\u7684\u8bf7\u6c42header\u548cbody\u56de\u663e\u51fa\u6765\uff09\u65b9\u4fbf\u6211\u4eec\u9a8c\u8bc1header\u7b49\u5b57\u6bb5\u7684\u4fee\u6539\u662f\u5426\u751f\u6548\uff0c\u975e\u5e38\u597d\u7528\u3002<\/p>\n<p>&nbsp;<\/p>\n<h5 id=\"id-\u6a21\u677f-\u53c2\u8003\u94fe\u63a5\uff1a\">\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.01happy.com\/golang-http-client-get-and-post\/\">golang\u4f7f\u7528http client\u53d1\u8d77get\u548cpost\u8bf7\u6c42\u793a\u4f8b<\/a><\/li>\n<li><a href=\"http:\/\/dlintw.github.io\/gobyexample\/public\/http-client.html\">Go by Example: HTTP Client<\/a><\/li>\n<li><a href=\"https:\/\/golang.org\/pkg\/net\/http\/\">Package http<\/a><\/li>\n<li><a href=\"https:\/\/www.kancloud.cn\/digest\/batu-go\/153529\">http\u5ba2\u6237\u7aef<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u7f18\u7531\uff1a \u5b66\u4e60\u548c\u6574\u7406\u4e00\u4e0b\u5982\u4f55\u4f7f\u7528Go \u8bed\u8a00\u53d1\u9001HTTP\u8bf7\u6c42\uff0c\u65b9\u4fbf\u4ee5\u540e\u4f7f\u7528\u548c\u53c2\u8003\u3002 \u6b63\u6587\uff1a \u53c2\u8003\u89e3 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,7,12],"tags":[21,564,22,24],"class_list":["post-4144","post","type-post","status-publish","format-standard","hentry","category-knowledgebase-2","category-programing","category-tools","tag-get","tag-golang","tag-http","tag-post"],"views":4139,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4144","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=4144"}],"version-history":[{"count":2,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4144\/revisions"}],"predecessor-version":[{"id":4148,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/4144\/revisions\/4148"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=4144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=4144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=4144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}