OSDN Git Service

net/http: delete temporary files.
[pf3gnuchains/gcc-fork.git] / libgo / go / net / http / doc.go
1 // Copyright 2011 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 /*
6 Package http provides HTTP client and server implementations.
7
8 Get, Head, Post, and PostForm make HTTP requests:
9
10         resp, err := http.Get("http://example.com/")
11         ...
12         resp, err := http.Post("http://example.com/upload", "image/jpeg", &buf)
13         ...
14         resp, err := http.PostForm("http://example.com/form",
15                 url.Values{"key": {"Value"}, "id": {"123"}})
16
17 The client must close the response body when finished with it:
18
19         resp, err := http.Get("http://example.com/")
20         if err != nil {
21                 // handle error
22         }
23         defer resp.Body.Close()
24         body, err := ioutil.ReadAll(resp.Body)
25         // ...
26
27 For control over HTTP client headers, redirect policy, and other
28 settings, create a Client:
29
30         client := &http.Client{
31                 CheckRedirect: redirectPolicyFunc,
32         }
33
34         resp, err := client.Get("http://example.com")
35         // ...
36
37         req := http.NewRequest("GET", "http://example.com", nil)
38         req.Header.Add("If-None-Match", `W/"wyzzy"`)
39         resp, err := client.Do(req)
40         // ...
41
42 For control over proxies, TLS configuration, keep-alives,
43 compression, and other settings, create a Transport:
44
45         tr := &http.Transport{
46                 TLSClientConfig:    &tls.Config{RootCAs: pool},
47                 DisableCompression: true,
48         }
49         client := &http.Client{Transport: tr}
50         resp, err := client.Get("https://example.com")
51
52 Clients and Transports are safe for concurrent use by multiple
53 goroutines and for efficiency should only be created once and re-used.
54
55 ListenAndServe starts an HTTP server with a given address and handler.
56 The handler is usually nil, which means to use DefaultServeMux.
57 Handle and HandleFunc add handlers to DefaultServeMux:
58
59         http.Handle("/foo", fooHandler)
60
61         http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
62                 fmt.Fprintf(w, "Hello, %q", html.EscapeString(r.URL.RawPath))
63         })
64
65         log.Fatal(http.ListenAndServe(":8080", nil))
66
67 More control over the server's behavior is available by creating a
68 custom Server:
69
70         s := &http.Server{
71                 Addr:           ":8080",
72                 Handler:        myHandler,
73                 ReadTimeout:    10e9,
74                 WriteTimeout:   10e9,
75                 MaxHeaderBytes: 1 << 20,
76         }
77         log.Fatal(s.ListenAndServe())
78 */
79 package http