Go语言网络编程-v0.0.2


Go语言网络编程 why404@七牛云存储 2012/07/21 1 Overview Socket HTTP RPC JSON JSON-RPC Web Devlopment 2 Socket 编程 3 HTTP 编程 HTTP Client HTTP Server 4 HTTP Client Methods http.Get http.Post http.PostForm http.Head (*http.Client).Do 5 http.Get func (c *Client) Get(url string) (r *Response, err error) resp, err := http.Get("http://example.com/") if err != nil { // handle error … return } defer resp.Body.close() io.Copy(os.Stdout, resp.Body) // Get is a wrapper around http.DefaultClient.Get 6 http.Post func (c *Client) Post(url string, bodyType string, body io.Reader) (r *Response, err error) resp, err := http.Post("http://example.com/upload", "image/jpeg", &imageDataBuf) if err != nil { // handle error … return } if resp.StatusCode != http.StatusOK { // handle error … return } // … // Post is a wrapper around http.DefaultClient.Post 7 http.PostForm func (c *Client) PostForm(url string, data url.Values) (r *Response, err error) resp, err := http.PostForm("http://example.com/posts", url.Values{"title": {"article title"}, "content": {"article body"}}) if err != nil { // handle error … return } // … // PostForm is a wrapper around http.DefaultClient.PostForm 8 http.Head func (c *Client) Head(url string) (r *Response, err error) resp, err := http.Head("http://example.com/") // Head is a wrapper around http.DefaultClient.Head 9 (*http.Client).Do func (c *Client) Do(req *Request) (resp *Response, err error) req, err := http.NewRequest("GET", "http://example.com", nil) // ... req.Header.Add("User-Agent", "Gobook Custom User-Agent") client := &http.Client{ //... } resp, err := client.Do(req) // ... 10 HTTP Server 处理 http 请求 自定义 http.Server 11 处理 http 请求 func ListenAndServe(addr string, handler Handler) error 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)) // http.Handle 或 http.HandleFunc 缺省注入 http.DefaultServeMux 12 自定义 http.Server s := &http.Server{ Addr: ":8080", Handler: myHandler, ReadTimeout: 10 * time.Second, WriteTimeout: 10 * time.Second, MaxHeaderBytes: 1 << 20, } log.Fatal(s.ListenAndServe()) 13 HTTP 高级话题 14 自定义 http.Client // in the “net/http” package var DefaultClient = &Client{} // s o .. http.Get == http.DefaultClient.Get, ... 15 http.Client Struct type Client struct { // Transport 指定了执行⼀个 HTTP 请求的运行机制 // http.DefaultTransport // http.RoundTripper Transport RoundTripper CheckRedirect func(req *Request, via []*Request) error Jar CookieJar } 16 自定义 http.Client 好处 发送自定义 HTTP Headers 改写重定向策略 etc. 17 定义具体的 http.Client client := &http.Client{ CheckRedirect: redirectPolicyFunc, } resp, err := client.Get("http://example.com") // … req, err := http.NewRequest("GET", "http://example.com", nil) // … req.Header.Add("User-Agent", "Our Custom User-Agent") req.Header.Add("If-None-Match", `W/"TheFileEtag"`) resp, err := client.Do(req) // … 18 http.Transport http.Transport 对象指定了执行⼀个 HTTP 请 求时的运行规则 实现了 http.RoundTripper 接口 19
还剩18页未读

继续阅读

下载pdf到电脑,查找使用更方便

pdf的实际排版效果,会与网站的显示效果略有不同!!

需要 8 金币 [ 分享pdf获得金币 ] 1 人已下载

下载pdf

pdf贡献者

kyo19

贡献于2015-09-11

下载需要 8 金币 [金币充值 ]
亲,您也可以通过 分享原创pdf 来获得金币奖励!
下载pdf