Go HTTP GET/POST request(转载)

ApiPost-宣博文 后端 2021-08-25

————————————————

版权声明:本文为 简书 博主「ADADACHAN」的原创文章。

原文链接:https://www.jianshu.com/p/30ab2df0f4d8

HTTP

超文本传输协议 (HTTP) 是一种用于分布式协作超媒体信息系统的应用协议。 HTTP 协议是万维网数据通信的基础。

 学习web,必须回http协议,万变不离其宗,http 基本请求都会有get和post方式。

获取get

HTTP GET 方法请求指定资源的表示。使用 GET 的请求应该只检索数据。

HTTP POST

HTTP POST 方法将数据发送到服务器。它通常用于上传文件或提交完整的 Web 表单。

go http

在 Go 中,我们使用 http 包来创建 GET 和 POST 请求。该包提供 HTTP 客户端和服务器实现。

go  GET 请求

以下示例在 Go 中创建一个简单的 GET 请求。

通过使用自己的账号

package main

import (

"fmt"

"io/ioutil"

"log"

"net/http"

)

func main() {

resp, err :=http.Get("https://www.jianshu.com/u/jianshuaccount")

if err !=nil {

log.Fatal(err)

}

defer resp.Body.Close()

body, err :=ioutil.ReadAll(resp.Body)

if err !=nil {

log.Fatal(err)

}

fmt.Println(string(body))

}

抓取的get的页面内容如下:

GOROOT=D:\Program Files\Go #gosetup

GOPATH=D:\Users\dev\sdk\go1.15.14 #gosetup

"D:\Program Files\Go\bin\go.exe" build -o C:\Users\Dev\AppData\Local\Temp\GoLand\___go_build_get_go.exe D:\gowork\beegoDemo\src\main\get.go #gosetup

C:\Users\Dev\AppData\Local\Temp\GoLand\___go_build_get_go.exe #gosetup

<!DOCTYPE html>

<!--[if IE 6]><html class="ie lt-ie8"><![endif]-->

<!--[if IE 7]><html class="ie lt-ie8"><![endif]-->

<!--[if IE 8]><html class="ie ie8"><![endif]-->

<!--[if IE 9]><html class="ie ie9"><![endif]-->

<!--[if !IE]><!--> <html> <!--<![endif]-->

<head>

  <meta charset="utf-8">

  <meta http-equiv="X-UA-Compatible" content="IE=Edge">

  <meta name="viewport" content="width=device-width, initial-scale=1.0,user-scalable=no">

  <!-- Start of Baidu Transcode -->

  <meta http-equiv="Cache-Control" content="no-siteapp" />

  <meta http-equiv="Cache-Control" content="no-transform" />

  <meta name="applicable-device" content="pc,mobile">

  <meta name="MobileOptimized" content="width"/>

  <meta name="HandheldFriendly" content="true"/>

  <meta name="mobile-agent" content="format=html5;url=https://www.jianshu.com/u/0e95708b1552">

  <!-- End of Baidu Transcode -->

    <meta name="description"  content="Technology is endless, life is endless">

  <meta name="tencent-site-verification" content="39a5ed77a02c0103af6ac08addbc3851"/>

  <meta name="360-site-verification" content="604a14b53c6b871206001285921e81d8" />

  <meta property="wb:webmaster" content="294ec9de89e7fadb" />

  <meta property="qc:admins" content="104102651453316562112116375" />

  <meta property="qc:admins" content="11635613706305617" />

  <meta property="qc:admins" content="1163561616621163056375" />

  <meta name="google-site-verification" content="6ARJIxhZLIgZT7J8MZkENr5mR0-CqshgzYyA3r3jBWU" />

  <meta http-equiv="mobile-agent" content="format=html5; url=https://www.jianshu.com/u/XXXXXXX">

  <!-- Apple -->

  <meta name="apple-mobile-web-app-title" content="简书">

    <title>ADADACHAN - 简书</title>

  <meta name="csrf-param" content="authenticity_token" />

<meta name="csrf-token" content="XXXXXX />

  <!--<script data-ad-client="ca-pub-3077285224019295" async target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://img.cdn.apipost.cn/mp/dd0971d693af800d.webp"></script>-->

  <link rel="stylesheet" media="all" target="_blank" href="https://url.apipost.cn/url?%3Ca+href%3D" rel="external nofollow" https://cdn2.jianshu.io/assets/web-9af87d737cccdeba08d6.css" rel="nofollow">https://cdn2.jianshu.io/assets/web-9af87d737cccdeba08d6.css" />

  <link rel="stylesheet" media="all" target="_blank" href="https://url.apipost.cn/url?%3Ca+href%3D" rel="external nofollow" https://cdn2.jianshu.io/assets/web/pages/users/show/entry-66c9a2b224e4181af887.css" rel="nofollow">https://cdn2.jianshu.io/assets/web/pages/users/show/entry-66c9a2b224e4181af887.css" />

  <link target="_blank" href="https://url.apipost.cn/url?%3Ca+href%3D" rel="external nofollow" https://cdn2.jianshu.io/assets/favicons/favicon-e743bfb1821442341c3ab15bdbe804f7ad97676bd07a770ccc9483473aa76f06.ico" rel="nofollow">https://cdn2.jianshu.io/assets/favicons/favicon-e743bfb1821442341c3ab15bdbe804f7ad97676bd07a770ccc9483473aa76f06.ico" rel="shortcut icon" type="image/x-icon">

      <link rel="apple-touch-icon-precomposed" target="_blank" href="https://url.apipost.cn/url?%3Ca+href%3D" rel="external nofollow" https://cdn2.jianshu.io/assets/apple-touch-icons/57-a6f1f1ee62ace44f6dc2f6a08575abd3c3b163288881c78dd8d75247682a4b27.png" rel="nofollow">https://cdn2.jianshu.io/assets/apple-touch-icons/57-a6f1f1ee62ace44f6dc2f6a08575abd3c3b163288881c78dd8d75247682a4b27.png" sizes="57x57" />

      <link rel="apple-touch-icon-precomposed" target="_blank" href="https://url.apipost.cn/url?%3Ca+href%3D" rel="external nofollow" https://cdn2.jianshu.io/assets/apple-touch-icons/72-fb9834bcfce738fd7b9c5e31363e79443e09a81a8e931170b58bc815387c1562.png" rel="nofollow">https://cdn2.jianshu.io/assets/apple-touch-icons/72-fb9834bcfce738fd7b9c5e31363e79443e09a81a8e931170b58bc815387c1562.png" sizes="72x72" />

      <link rel="apple-touch-icon-precomposed" target="_blank" href="https://url.apipost.cn/url?%3Ca+href%3D" rel="external nofollow" https://cdn2.jianshu.io/assets/apple-touch-icons/76-49d88e539ff2489475d603994988d871219141ecaa0b1a7a9a1914f4fe3182d6.png" rel="nofollow">https://cdn2.jianshu.io/assets/apple-touch-icons/76-49d88e539ff2489475d603994988d871219141ecaa0b1a7a9a1914f4fe3182d6.png" sizes="76x76" />

      <link rel="apple-touch-icon-precomposed" target="_blank" href="https://url.apipost.cn/url?%3Ca+href%3D" rel="external nofollow" https://cdn2.jianshu.io/assets/apple-touch-icons/114-24252fe693524ed3a9d0905e49bff3cbd0228f25a320aa09053c2ebb4955de97.png" rel="nofollow">https://cdn2.jianshu.io/assets/apple-touch-icons/114-24252fe693524ed3a9d0905e49bff3cbd0228f25a320aa09053c2ebb4955de97.png" sizes="114x114" />

      <link rel="apple-touch-icon-precomposed" target="_blank" href="https://url.apipost.cn/url?%3Ca+href%3D" rel="external nofollow" https://cdn2.jianshu.io/assets/apple-touch-icons/120-1bb7371f5e87f93ce780a5f1a05ff1b176828ee0d1d130e768575918a2e05834.png" rel="nofollow">https://cdn2.jianshu.io/assets/apple-touch-icons/120-1bb7371f5e87f93ce780a5f1a05ff1b176828ee0d1d130e768575918a2e05834.png" sizes="120x120" />

      <link rel="apple-touch-icon-precomposed" target="_blank" href="https://url.apipost.cn/url?%3Ca+href%3D" rel="external nofollow" https://cdn2.jianshu.io/assets/apple-touch-icons/152-bf209460fc1c17bfd3e2b84c8e758bc11ca3e570fd411c3bbd84149b97453b99.png" rel="nofollow">https://cdn2.jianshu.io/assets/apple-touch-icons/152-bf209460fc1c17bfd3e2b84c8e758bc11ca3e570fd411c3bbd84149b97453b99.png" sizes="152x152" />

  <!-- Start of 访问统计 -->

    <script>

    var _hmt = _hmt || [];

    (function() {

      var hm = document.createElement("script");

      hm.src = "//hm.baidu.com/hm.js?0c0e9d9b1e7d617b3e6842e85b9fb068";

      var s = document.getElementsByTagName("script")[0];

      s.parentNode.insertBefore(hm, s);

    })();

    (function () {

      var hm = document.createElement('script');

      hm.src = 'https://v1.cnzz.com/z_stat.php?id=xxxx&web_id=xxx';

      var s = document.getElementsByTagName('script')[0];

      s.parentNode.insertBefore(hm, s);

    }());

  </script>

  <!-- End of 访问统计 -->

</head>

  <!-- 只给10%的用户添加代码 -->

  <!-- ###第四范式-智能推荐:代码直接复制 无需修改参数### -->

  <!-- ###功能:上报内容并反馈用户行为### -->

  <!--

  -->

  <body lang="zh-CN" class="reader-black-font">

    <!-- 全局顶部导航栏 -->

<nav class="navbar navbar-default navbar-fixed-top" role="navigation">

  <div class="width-limit">

    <!-- 左上方 Logo -->

    <a class="logo" target="_blank" href="https://url.apipost.cn/url?%2F" rel="external nofollow" ><img target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://img.cdn.apipost.cn/mp/27adc892f3046e69.png" target="_blank" alt="Go HTTP GET/POST request(转载)" /></a>

    <!-- 右上角 -->

      <!-- 未登录显示登录/注册/写文章 -->

      <a class="btn write-btn" target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fwriter%23%2F" rel="external nofollow" >

        <i class="iconfont ic-write"></i>写文章

</a>      <a class="btn sign-up" id="sign_up" target="_blank" href="https://url.apipost.cn/url?%2Fsign_up" rel="external nofollow" >注册</a>

      <a class="btn log-in" id="sign_in" target="_blank" href="https://url.apipost.cn/url?%2Fsign_in" rel="external nofollow" >登录</a>

    <!-- 如果用户登录,显示下拉菜单 -->

    <div id="view-mode-ctrl">

    </div>

    <div class="container">

      <div class="navbar-header">

        <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#menu" aria-expanded="false">

          <span class="icon-bar"></span>

          <span class="icon-bar"></span>

          <span class="icon-bar"></span>

        </button>

      </div>

      <div class="collapse navbar-collapse" id="menu">

        <ul class="nav navbar-nav">

            <li class="tab ">

              <a target="_blank" href="https://url.apipost.cn/url?%2F" rel="external nofollow" >

                <span class="menu-text">首页</span><i class="iconfont ic-navigation-discover menu-icon"></i>

</a>            </li>

            <li class="tab ">

              <a id="web-nav-app-download-btn" class="app-download-btn" target="_blank" href="https://url.apipost.cn/url?%2Fapps%3Futm_medium%3Ddesktop%26amp%3Bamp%3Butm_source%3Dnavbar-apps" rel="external nofollow" ><span class="menu-text">下载App</span><i class="iconfont ic-navigation-download menu-icon"></i></a>

            </li>

            <li onclick="addPoint(event)" class="tab ">

              <a target="_blank" href="https://url.apipost.cn/url?%2Ftechareas" rel="external nofollow" >

                <span class="menu-text">IT技术</span><i style="transform:scale(1.3)" class="iconfont ic-chengxuyuanzhuanqu menu-icon"></i>

</a>            </li>

          <li class="search">

            <form target="_blank" action="/search" accept-charset="UTF-8" method="get"><input name="utf8" type="hidden" value="&#x2713;" />

              <input type="text" name="q" id="q" value="" autocomplete="off" placeholder="搜索" class="search-input" />

              <a class="search-btn" target="_blank" href="https://url.apipost.cn/url?javascript%3Avoid%28null%29" rel="external nofollow" ><i class="iconfont ic-search"></i></a>

</form>          </li>

        </ul>

      </div>

    </div>

  </div>

</nav>

<script type="text/javascript">

function addPoint (e) {

  e.stopPropagation();

  JsSensor.trackEvent(SAEVENTS.PC_HEADER_TAB_CLICK,{tab:'程序员专区',place:'首页'});

}

</script>

<div class="container person">

  <div class="row">

    <div class="col-xs-16 main">

      <div class="main-top">

  <a class="avatar" target="_blank" href="https://url.apipost.cn/url?%2Fu%2F0e95708b1552" rel="external nofollow" >

    <img target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://upload.jianshu.io/users/upload_avatars/16302180/8b41ff59-2a25-4fec-8f74-2f5966a0252e.png?imageMogr2/auto-orient/strip|imageView2/1/w/240/h/240" target="_blank" alt="Go HTTP GET/POST request(转载)" />

</a>

    <div class="follow-button" props-data-user-id="16302180" props-data-state="0"></div>

    <a class="btn btn-hollow" target="_blank" href="https://url.apipost.cn/url?%2Fnotifications%23%2Fchats%2Fnew%3Fmail_to%3D16302180" rel="external nofollow" >发简信</a>

  <div class="title">

    <a class="name" target="_blank" href="https://url.apipost.cn/url?%2Fu%2F0e95708b1552" rel="external nofollow" >ADADACHAN</a>

  </div>

  <div class="info">

    <ul>

      <li>

        <div class="meta-block">

          <a target="_blank" href="https://url.apipost.cn/url?%2Fusers%2F0e95708b1552%2Ffollowing" rel="external nofollow" >

            <p>3</p>

            关注 <i class="iconfont ic-arrow"></i>

</a>        </div>

      </li>

      <li>

        <div class="meta-block">

          <a target="_blank" href="https://url.apipost.cn/url?%2Fusers%2F0e95708b1552%2Ffollowers" rel="external nofollow" >

            <p>1</p>

            粉丝 <i class="iconfont ic-arrow"></i>

</a>        </div>

      </li>

      <li>

        <div class="meta-block">

          <a target="_blank" href="https://url.apipost.cn/url?%2Fu%2F0e95708b1552" rel="external nofollow" >

            <p>34</p>

            文章 <i class="iconfont ic-arrow"></i>

</a>        </div>

      </li>

      <li>

        <div class="meta-block">

          <p>36615</p>

          <div>字数</div>

        </div>

      </li>

      <li>

        <div class="meta-block">

          <p>31</p>

          <div>收获喜欢</div>

        </div>

      </li>

        <li>

          <div class="meta-block">

            <p>7</p>

            <div>总资产</div>

          </div>

        </li>

    </ul>

  </div>

</div>

      <ul class="trigger-menu" data-pjax-container="#list-container"><li class="active"><a target="_blank" href="https://url.apipost.cn/url?%2Fu%2F0e95708b1552%3Forder_by%3Dshared_at" rel="external nofollow" ><i class="iconfont ic-articles"></i> 文章</a></li><li class=""><a target="_blank" href="https://url.apipost.cn/url?%2Fusers%2F0e95708b1552%2Ftimeline" rel="external nofollow" ><i class="iconfont ic-feed"></i> 动态</a></li><li class=""><a target="_blank" href="https://url.apipost.cn/url?%2Fu%2F0e95708b1552%3Forder_by%3Dcommented_at" rel="external nofollow" ><i class="iconfont ic-latestcomments"></i> 最新评论</a></li><li class=""><a target="_blank" href="https://url.apipost.cn/url?%2Fu%2F0e95708b1552%3Forder_by%3Dtop" rel="external nofollow" ><i class="iconfont ic-hot"></i> 热门</a></li></ul>

      <div id="list-container">

        <ul class="note-list" infinite-scroll-url="/u/0e95708b1552?order_by=shared_at">

<li id="note-91967359" data-note-id="91967359" class="have-img">

    <a class="wrap-img" target="_blank" href="https://url.apipost.cn/url?%2Fp%2Fb6a8b3cf66a1" rel="external nofollow" target="_blank">

      <img data-echo="https://upload-images.jianshu.io/upload_images/16302180-68cba96eabec899c.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/300/h/240" class="img-blur" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://upload-images.jianshu.io/upload_images/16302180-68cba96eabec899c.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/150/h/120" target="_blank" alt="Go HTTP GET/POST request(转载)" />

    </a>

  <div class="content  ">

    <a class="title" target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2Fb6a8b3cf66a1" rel="external nofollow" >跳跳蛙早上好</a>

    <p class="abstract">

      1.看到跳跳蛙,心情就美丽 2.梦境和昨天看的电影基本一样,看来电影要多看才对,不是什么美梦,是做有故事情节的梦,有点再看一遍电影的感觉,是四维...

    </p>

    <div class="meta">

      <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2Fb6a8b3cf66a1" rel="external nofollow" >

        <i class="iconfont ic-list-read"></i> 15

</a>        <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2Fb6a8b3cf66a1%23comments" rel="external nofollow" >

          <i class="iconfont ic-list-comments"></i> 0

</a>      <span><i class="iconfont ic-list-like"></i> 1</span>

      <span class="time" data-shared-at="2021-08-23T08:03:08+08:00"></span>

    </div>

  </div>

</li>

<li id="note-91928236" data-note-id="91928236" class="have-img">

    <a class="wrap-img" target="_blank" href="https://url.apipost.cn/url?%2Fp%2Fddf209dee04a" rel="external nofollow" target="_blank">

      <img data-echo="https://upload-images.jianshu.io/upload_images/16302180-ab51f32761392f7d.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/300/h/240" class="img-blur" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://upload-images.jianshu.io/upload_images/16302180-ab51f32761392f7d.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/150/h/120" target="_blank" alt="Go HTTP GET/POST request(转载)" />

    </a>

  <div class="content  ">

    <a class="title" target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2Fddf209dee04a" rel="external nofollow" >IDE移动硬盘</a>

    <p class="abstract">

      1.从北京幸幸苦苦背下来的移动硬盘,有3块,2个是老式硬盘,都是80g的,这个小的,应该里面有源码,需要找到旧式移动硬盘盒纸。 2.昨天的十公里...

    </p>

    <div class="meta">

      <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2Fddf209dee04a" rel="external nofollow" >

        <i class="iconfont ic-list-read"></i> 15

</a>        <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2Fddf209dee04a%23comments" rel="external nofollow" >

          <i class="iconfont ic-list-comments"></i> 0

</a>      <span><i class="iconfont ic-list-like"></i> 0</span>

      <span class="time" data-shared-at="2021-08-22T07:51:41+08:00"></span>

    </div>

  </div>

</li>

    <div aria-label="3rd-ad">

    <script type="text/javascript" smua="d=p&amp;s=b&amp;u=u3179110&amp;w=625&amp;h=140" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://img.cdn.apipost.cn/mp/e8576e077fc1089a.webp"></script>

    <script type="text/javascript">

    setTimeout(function(){

      if(window.JsSensor){

          JsSensor.trackEvent(SAEVENTS.PC_AD_SHOW, {

          page: '他人主页',

          place: '信息流广告'+ 1,

          uid: 'u3179110'

        });

      }

    },1500)

    </script>

</div>

<li id="note-91891317" data-note-id="91891317" class="have-img">

    <a class="wrap-img" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F98180f2757c9" rel="external nofollow" target="_blank">

      <img data-echo="https://upload-images.jianshu.io/upload_images/16302180-6101fb0c7f420913.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/300/h/240" class="img-blur" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://upload-images.jianshu.io/upload_images/16302180-6101fb0c7f420913.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/150/h/120" target="_blank" alt="Go HTTP GET/POST request(转载)" />

    </a>

  <div class="content  ">

    <a class="title" target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F98180f2757c9" rel="external nofollow" >快9月,改变从头开始创新</a>

    <p class="abstract">

      1.今天体力不支,跑10公里,结果是走了快5公里,要有好身体,首先是不可以感冒。最近感冒快一整天,吃了一点药,大家都知道,生病体能会下降的,想想...

    </p>

    <div class="meta">

      <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F98180f2757c9" rel="external nofollow" >

        <i class="iconfont ic-list-read"></i> 24

</a>        <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F98180f2757c9%23comments" rel="external nofollow" >

          <i class="iconfont ic-list-comments"></i> 0

</a>      <span><i class="iconfont ic-list-like"></i> 0</span>

      <span class="time" data-shared-at="2021-08-21T09:42:01+08:00"></span>

    </div>

  </div>

</li>

<li id="note-91847660" data-note-id="91847660" class="have-img">

    <a class="wrap-img" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F00316a2b2722" rel="external nofollow" target="_blank">

      <img data-echo="https://upload-images.jianshu.io/upload_images/16302180-75803db38ba9da16.png?imageMogr2/auto-orient/strip|imageView2/1/w/300/h/240" class="img-blur" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://upload-images.jianshu.io/upload_images/16302180-75803db38ba9da16.png?imageMogr2/auto-orient/strip|imageView2/1/w/150/h/120" target="_blank" alt="Go HTTP GET/POST request(转载)" />

    </a>

  <div class="content  ">

    <a class="title" target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F00316a2b2722" rel="external nofollow" >意外,程序员是农民工</a>

    <p class="abstract">

      1.北京这个城市,程序员就是农民工,一份工作,拆分的特别细来执行,明明可以一个人干完的,不用,非的拆分一堆细细的螺丝钉来做,美其名曰是精致,其实...

    </p>

    <div class="meta">

        <span class="jsd-meta">

          <i class="iconfont ic-paid1"></i> 0.2

        </span>

      <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F00316a2b2722" rel="external nofollow" >

        <i class="iconfont ic-list-read"></i> 21

</a>        <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F00316a2b2722%23comments" rel="external nofollow" >

          <i class="iconfont ic-list-comments"></i> 0

</a>      <span><i class="iconfont ic-list-like"></i> 1</span>

      <span class="time" data-shared-at="2021-08-20T07:36:21+08:00"></span>

    </div>

  </div>

</li>

<li id="note-91804918" data-note-id="91804918" class="have-img">

    <a class="wrap-img" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F18445f1a5e97" rel="external nofollow" target="_blank">

      <img data-echo="https://upload-images.jianshu.io/upload_images/16302180-a579087a26c2f3cb.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/300/h/240" class="img-blur" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://upload-images.jianshu.io/upload_images/16302180-a579087a26c2f3cb.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/150/h/120" target="_blank" alt="Go HTTP GET/POST request(转载)" />

    </a>

  <div class="content  ">

    <a class="title" target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F18445f1a5e97" rel="external nofollow" >一天从瑜伽开始</a>

    <p class="abstract">

      1.一起来先喝杯水,根据昨天是否上火,和温水还是凉水,如何上火,直接来杯凉水,一杯直接喝不用分多次,如果有口疮,可以加点盐巴漱口,不喝 温水基本...

    </p>

    <div class="meta">

        <span class="jsd-meta">

          <i class="iconfont ic-paid1"></i> 0.2

        </span>

      <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F18445f1a5e97" rel="external nofollow" >

        <i class="iconfont ic-list-read"></i> 16

</a>        <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F18445f1a5e97%23comments" rel="external nofollow" >

          <i class="iconfont ic-list-comments"></i> 1

</a>      <span><i class="iconfont ic-list-like"></i> 1</span>

      <span class="time" data-shared-at="2021-08-19T07:44:16+08:00"></span>

    </div>

  </div>

</li>

<li id="note-91762635" data-note-id="91762635" class="have-img">

    <a class="wrap-img" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F812cb4a06bce" rel="external nofollow" target="_blank">

      <img data-echo="https://upload-images.jianshu.io/upload_images/16302180-1d10ca0952d07086.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/300/h/240" class="img-blur" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://upload-images.jianshu.io/upload_images/16302180-1d10ca0952d07086.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/150/h/120" target="_blank" alt="Go HTTP GET/POST request(转载)" />

    </a>

  <div class="content  ">

    <a class="title" target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F812cb4a06bce" rel="external nofollow" >梦空间</a>

    <p class="abstract">

      晚上喝了一点酒,不多一杯,主要是帮助睡眠,结果是适得其反,反而睡不好。 先吃个早饭,简简单单的 梦空间是什么?我就是在进行建筑,感觉在设计什么?...

    </p>

    <div class="meta">

        <span class="jsd-meta">

          <i class="iconfont ic-paid1"></i> 0.2

        </span>

      <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F812cb4a06bce" rel="external nofollow" >

        <i class="iconfont ic-list-read"></i> 29

</a>        <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F812cb4a06bce%23comments" rel="external nofollow" >

          <i class="iconfont ic-list-comments"></i> 1

</a>      <span><i class="iconfont ic-list-like"></i> 1</span>

      <span class="time" data-shared-at="2021-08-18T08:29:18+08:00"></span>

    </div>

  </div>

</li>

<li id="note-91735411" data-note-id="91735411" class="">

  <div class="content  ">

    <a class="title" target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F283d9f265e77" rel="external nofollow" >使用 Pytest 进行有效的 Python 测试</a>

    <p class="abstract">

      使用 Pytest 进行有效的 Python 测试 测试您的代码会带来各种各样的好处。它增加了您对代码按预期运行的信心,并确保对代码的更改不会导...

    </p>

    <div class="meta">

        <span class="jsd-meta">

          <i class="iconfont ic-paid1"></i> 0.2

        </span>

      <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F283d9f265e77" rel="external nofollow" >

        <i class="iconfont ic-list-read"></i> 15

</a>        <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F283d9f265e77%23comments" rel="external nofollow" >

          <i class="iconfont ic-list-comments"></i> 1

</a>      <span><i class="iconfont ic-list-like"></i> 1</span>

      <span class="time" data-shared-at="2021-08-17T17:29:42+08:00"></span>

    </div>

  </div>

</li>

    <div aria-label="3rd-ad">

    <script type="text/javascript" smua="d=p&amp;s=b&amp;u=u3179110&amp;w=625&amp;h=140" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://img.cdn.apipost.cn/mp/e8576e077fc1089a.webp"></script>

    <script type="text/javascript">

    setTimeout(function(){

      if(window.JsSensor){

          JsSensor.trackEvent(SAEVENTS.PC_AD_SHOW, {

          page: '他人主页',

          place: '信息流广告'+ 2,

          uid: 'u3179110'

        });

      }

    },1500)

    </script>

</div>

<li id="note-91719158" data-note-id="91719158" class="have-img">

    <a class="wrap-img" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F1a654412bd76" rel="external nofollow" target="_blank">

      <img data-echo="https://upload-images.jianshu.io/upload_images/16302180-27b5d38732a90443.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/300/h/240" class="img-blur" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://upload-images.jianshu.io/upload_images/16302180-27b5d38732a90443.jpg?imageMogr2/auto-orient/strip|imageView2/1/w/150/h/120" target="_blank" alt="Go HTTP GET/POST request(转载)" />

    </a>

  <div class="content  ">

    <a class="title" target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F1a654412bd76" rel="external nofollow" >简单的一天从抓bug开始</a>

    <p class="abstract">

      1.早上起来看到一只小强 这种小强应该在北方才对,南方应该比这个更大只才对,拍完这个都没有胃口吃饭了,就当作减肥吧。 2.早起清单的一碗绿豆粥开...

    </p>

    <div class="meta">

        <span class="jsd-meta">

          <i class="iconfont ic-paid1"></i> 0.2

        </span>

      <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F1a654412bd76" rel="external nofollow" >

        <i class="iconfont ic-list-read"></i> 14

</a>        <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2F1a654412bd76%23comments" rel="external nofollow" >

          <i class="iconfont ic-list-comments"></i> 0

</a>      <span><i class="iconfont ic-list-like"></i> 1</span>

      <span class="time" data-shared-at="2021-08-17T08:05:13+08:00"></span>

    </div>

  </div>

</li>

<li id="note-91680874" data-note-id="91680874" class="">

  <div class="content  ">

    <a class="title" target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2Fa54134e3a67f" rel="external nofollow" >为什么要构架师?</a>

    <p class="abstract">

      为什么要构架师? 1.企业发展的阶段,需要给出企业的构架未来的设计和发展方向定义和规划,简单讲就是国家的5年计划设计师 那么企业中的构架师,当然...

    </p>

    <div class="meta">

      <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2Fa54134e3a67f" rel="external nofollow" >

        <i class="iconfont ic-list-read"></i> 14

</a>        <a target="_blank" target="_blank" href="https://url.apipost.cn/url?%2Fp%2Fa54134e3a67f%23comments" rel="external nofollow" >

          <i class="iconfont ic-list-comments"></i> 0

</a>      <span><i class="iconfont ic-list-like"></i> 0</span>

      <span class="time" data-shared-at="2021-08-16T09:53:49+08:00"></span>

    </div>

  </div>

</li>

</ul>

      </div>

    </div>

<div class="col-xs-7 col-xs-offset-1 aside">

      <div class="title">个人介绍</div>

  <div class="description">

    <div class="js-intro">Technology is endless, life is endless</div>

    <a class="social-icon-sprite social-icon-index" target="_blank" target="_blank" href="https://url.apipost.cn/url?%3Ca+href%3D" rel="external nofollow" https://links.jianshu.com/go?to=https://ai-keny.blog.csdn.net/" rel="nofollow">https://links.jianshu.com/go?to=https://ai-keny.blog.csdn.net/"></a>

  </div>

  <!-- 广告 -->

    <div data-vcomp="third-party-ad" props-data-page="他人主页" props-data-place="侧边栏上方广告" props-data-smua="u2823523" props-data-script-url="https://img.cdn.apipost.cn/mp/e8576e077fc1089a.webp" props-data-w="280" props-data-h="280"></div>

  <div class="publication-list"></div>

  <div class="course-list"></div>

  <ul class="list user-dynamic">

    <li>

      <a target="_blank" href="https://url.apipost.cn/url?%2Fusers%2F0e95708b1552%2Fsubscriptions" rel="external nofollow" >

        <i class="iconfont ic-collection"></i> <span>他关注的专题/文集/连载</span>

</a>    </li>

    <li>

      <a target="_blank" href="https://url.apipost.cn/url?%2Fusers%2F0e95708b1552%2Fliked_notes" rel="external nofollow" >

        <i class="iconfont ic-like"></i> <span>他喜欢的文章</span>

</a>    </li>

  </ul>

  <!-- 专题和文集 -->

  <div class="js-collection-and-notebook-container"></div>

  <!-- 广告 -->

    <div data-vcomp="third-party-ad" props-data-page="他人主页" props-data-place="侧边栏下方广告" props-data-smua="u2823523" props-data-script-url="https://img.cdn.apipost.cn/mp/e8576e077fc1089a.webp" props-data-w="280" props-data-h="280"></div>

</div>

  </div>

</div>

<div data-vcomp="side-tool"></div>

    <script type="application/json" data-name="page-data">{"user_signed_in":false,"locale":"zh-CN","os":"other","read_mode":"day","read_font":"font2","user":{"slug":"0e95708b1552","gender":3},"has_collections":true}</script>

    <script target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://img.cdn.apipost.cn/mp/81f6a447c40761ab.webp" crossorigin="anonymous"></script>

    <script target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://img.cdn.apipost.cn/mp/0e3ef1b6676b240b.webp" crossorigin="anonymous"></script>

<script target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://img.cdn.apipost.cn/mp/5b088ab39edd0965.webp" crossorigin="anonymous"></script>

    <script target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" target="_blank" src="https://img.cdn.apipost.cn/mp/14421e0d85ce494c.webp" crossorigin="anonymous"></script>

  </body>

</html>

Process finished with the exit code 0

可以看出来,<html></html>里面就是我首页的内容,内容的主要id,我屏蔽了,感兴趣的同学,可以直接抓一下。

2.异步请求

package main

import (

"fmt"

"io/ioutil"

"log"

"net/http"

"regexp"

"sync"

)

func main() {

urls := []string{

"http://webcode.me",

      "https://example.com",

      "http://httpbin.org",

      "https://www.perl.org",

      "https://www.php.net",

      "https://www.python.org",

      "https://code.visualstudio.com",

      "https://clojure.org",

      "https://www.baidu.com",

  }

var wgsync.WaitGroup

  for _, u :=range urls {

wg.Add(1)

go func(urlstring) {

defer wg.Done()

content :=doReq(url)

title :=getTitle(content)

fmt.Println(title)

}(u)

}

wg.Wait()

}

func doReq(urlstring) (contentstring) {

resp, err :=http.Get(url)

defer resp.Body.Close()

body, err :=ioutil.ReadAll(resp.Body)

if err !=nil {

log.Fatal(err)

}

return string(body)

}

func getTitle(contentstring) (titlestring) {

re :=regexp.MustCompile("<title>(.*)</title>")

parts := re.FindStringSubmatch(content)

if len(parts) >0 {

return parts[1]

}else {

return "no title"

  }

}

我们发出多个异步 HTTP 请求。我们得到每个网页的标题标签的内容。

The Perl Programming Language - www.perl.org

My html page

httpbin.org

Clojure

Example Domain

PHP: Hypertext Preprocessor

Welcome to Python.org

Visual Studio Code - Code Editing. Redefined

百度一下,你就知道

3.带有查询参数的 GET 请求

package main

import (

"fmt"

"io/ioutil"

"log"

"net/http"

"net/url"

)

func main() {

name :="kenychen"

  occupation :="gardener"

  params :="name=" +url.QueryEscape(name) +"&" +

"occupation=" +url.QueryEscape(occupation)

path :=fmt.Sprintf("https://httpbin.org/get?%s", params)

resp, err :=http.Get(path)

if err !=nil {

log.Fatal(err)

}

defer resp.Body.Close()

body, err :=ioutil.ReadAll(resp.Body)

if err !=nil {

log.Fatal(err)

}

fmt.Println(string(body))

}

运行结果

{

  "args": {

    "name": "kenychen",

    "occupation": "gardener"

  },

  "headers": {

    "Accept-Encoding": "gzip",

    "Host": "httpbin.org",

    "User-Agent": "Go-http-client/2.0",

    "X-Amzn-Trace-Id": "Root=1-6123339d-1c30bc7a15b0f90200337e4a"

  },

  "origin": "20.44.231.34",

  "url": "https://httpbin.org/get?name=kenychen&occupation=gardener"

}

Go HTTP POST 请求 FORM 数据

package main

import (

"encoding/json"

"fmt"

"log"

"net/http"

"net/url"

)

func main() {

data :=url.Values{

"name":      {"kenychen"},

      "occupation": {"gardener"},

  }

resp, err :=http.PostForm("https://httpbin.org/post", data)

if err !=nil {

log.Fatal(err)

}

var resmap[string]interface{}

json.NewDecoder(resp.Body).Decode(&res)

fmt.Println(res["form"])

}

运行效果:

"D:\Program Files\Go\bin\go.exe" build -o C:\Users\Dev\AppData\Local\Temp\GoLand\___go_build_post_req_form_go.exe D:\gowork\beegoDemo\src\main\post_req_form.go #gosetup

C:\Users\Dev\AppData\Local\Temp\GoLand\___go_build_post_req_form_go.exe #gosetup

map[name:kenychen occupation:gardener]

Go HTTP POST request JSON data

import (

    "bytes"

    "encoding/json"

    "fmt"

    "log"

    "net/http"

)

func main() {

    values := map[string]string{"name": "kenychen", "occupation": "gardener"}

    json_data, err := json.Marshal(values)

    if err != nil {

        log.Fatal(err)

    }

    resp, err := http.Post("https://httpbin.org/post", "application/json",

        bytes.NewBuffer(json_data))

    if err != nil {

        log.Fatal(err)

    }

    var res map[string]interface{}

    json.NewDecoder(resp.Body).Decode(&res)

    fmt.Println(res["json"])

}

Go HTTP GET/POST request(转载)

作者:ADADACHAN

链接:https://www.jianshu.com/p/30ab2df0f4d8

来源:简书

著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

Apipost 私有化火热进行中

评论