<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                官方文檔:[https://golang.org/pkg/net/http/](https://golang.org/pkg/net/http/) # http客戶端 http包提供客戶端與服務端的能力 Get、Head、Post和PostForm發出HTTP(或HTTPS)請求: ~~~ resp, err := http.Get("http://example.com/") ... resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf) ... resp, err := http.PostForm("http://example.com/form", url.Values{"key": {"Value"}, "id": {"123"}}) ~~~ 完成響應正文后,客戶端必須關閉它: ~~~ resp, err := http.Get("http://example.com/") if err != nil { // handle error } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) // ... ~~~ 要控制HTTP客戶端請求頭、重定向策略和其他設置,請創建客戶端: ~~~ client := &http.Client{ CheckRedirect: redirectPolicyFunc, } resp, err := client.Get("http://example.com") // ... req, err := http.NewRequest("GET", "http://example.com", nil) // ... req.Header.Add("If-None-Match", `W/"wyzzy"`) resp, err := client.Do(req) // ... ~~~ 要控制代理、TLS配置、保持有效、壓縮和其他設置,請創建傳輸: ~~~ tr := &http.Transport{ MaxIdleConns: 10, IdleConnTimeout: 30 * time.Second, DisableCompression: true, } client := &http.Client{Transport: tr} resp, err := client.Get("https://example.com") ~~~ 客戶端和傳輸對于多個goroutine并發使用是安全的,為了提高效率,應該只創建一次并重新使用。 # http服務器 一個示例 ```golang import ( "log" "net/http" ) // GET /hello1 func hello(writer http.ResponseWriter, request *http.Request) { writer.Write([]byte("hello!")) } func main() { mux := &http.ServeMux{} mux.HandleFunc("/hello1", hello) // 路由 log.Fatal(http.ListenAndServe(":8080", mux)) // 啟動服務 } ``` 敲入`go run app.go`運行 訪問[http://localhost:8080/hello1](http://localhost:8080/hello1) 輸出`hello!` ## server ListenAndServe使用給定的地址和處理程序啟動HTTP服務器。通常情況下,emuhandler的意思是nilx。Handle和HandleFunc向DefaultServeMux添加處理程序: ~~~ http.Handle("/foo", fooHandler) http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.Path)) }) log.Fatal(http.ListenAndServe(":8080", nil)) ~~~ 通過創建自定義服務器,可以更好地控制服務器的行為: ~~~ s := &http.Server{ Addr: ":8080", Handler: myHandler, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } log.Fatal(s.ListenAndServe()) ~~~ 從Go1.6開始,當使用HTTPS時,http包對http/2協議有透明的支持。必須禁用HTTP/2的程序可以通過設置傳輸.TLSNextProto(針對客戶)或服務器.TLSNextProto(對于服務器)到非零的空映射。或者,當前支持以下 GODEBUG環境變量: ~~~ GODEBUG=http2client=0 # disable HTTP/2 client support GODEBUG=http2server=0 # disable HTTP/2 server support GODEBUG=http2debug=1 # enable verbose HTTP/2 debug logs GODEBUG=http2debug=2 # ... even more verbose, with frame dumps ~~~ ## 靜態文件服務器 ```golang http.Handle("/tmpfiles/", http.StripPrefix("/tmpfiles/", http.FileServer(http.Dir("/tmp")))) ``` 訪問`http://localhost:8080/tmpfiles` 查看效果
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看