关于go:Go-快速入门指南-HTTP

2次阅读

共计 937 个字符,预计需要花费 3 分钟才能阅读完成。

概述

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>
*/

分割我

正文完
 0