HTTP

概述 #

net/http 包含了 HTTP 相关方法。

例子 #

package main

import (
	"errors"
	"fmt"
	"io"
	"net/http"
)

func main() {
	resp, err := http.Get("https://www.baidu.com")
	if err != nil {
		panic(err)
	}
	defer func() {
		err = resp.Body.Close()
		if err != nil {
			fmt.Printf("resp.Body.Close() %s", err)
		}
	}()

	fmt.Printf("Response status code = %d\n", resp.StatusCode)
	fmt.Printf("Response content type = %s\n", resp.Header.Get("Content-Type"))
	fmt.Printf("Response content length = %d\n", resp.ContentLength)

	body := make([]byte, resp.ContentLength)
	n, err := resp.Body.Read(body)
	if err != nil && !errors.Is(err, io.EOF) {
		panic(err)
	}

	fmt.Printf("Response body read = %d\n", n)
	fmt.Printf("Response body = %s\n", body)
}

// $ go run main.go
// 输出如下
/**
  Response status code = 200
  Response content type = text/html
  Response content length = 227
  Response body read = 227
  Response body = <html>
  <head>
          <script>
                  location.replace(location.href.replace("https://","http://"));
          </script>
  </head>
  <body>
          <noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript>
  </body>
  </html>
*/

转载申请

本作品采用 知识共享署名 4.0 国际许可协议 进行许可,转载时请注明原文链接,图片在使用时请保留全部内容,商业转载请联系作者获得授权。

© 蛮荆 | 陕公网安备 61011302001681 号 | 陕ICP备2023004378号-1 | Rendered by Hugo