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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                在下邊這個程序中,數組中的url都將被訪問:會發送一個簡單的`http.Head()`請求查看返回值;它的聲明如下:`func Head(url string) (r *Response, err error)` 返回狀態碼會被打印出來。 示例 15.7?[poll_url.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_15/poll_url.go): ~~~ package main import ( "fmt" "net/http" ) var urls = []string{ "http://www.google.com/", "http://golang.org/", "http://blog.golang.org/", } func main() { // Execute an HTTP HEAD request for all url's // and returns the HTTP status string or an error string. for _, url := range urls { resp, err := http.Head(url) if err != nil { fmt.Println("Error:", url, err) } fmt.Print(url, ": ", resp.Status) } } ~~~ 輸出為: ~~~ http://www.google.com/ : 302 Found http://golang.org/ : 200 OK http://blog.golang.org/ : 200 OK ~~~ *譯者注*?由于國內的網絡環境現狀,很有可能見到如下超時錯誤提示: ~~~ Error: http://www.google.com/ Head http://www.google.com/: dial tcp 216.58.221.100:80: connectex: A connection attempt failed because the connected pa rty did not properly respond after a period of time, or established connection failed because connected host has failed to respond. ~~~ 在下邊的程序中我們使用`http.Get()`獲取網頁內容;?`Get`的返回值`res`中的`Body`屬性包含了網頁內容,然后我們用`ioutil.ReadAll`把它讀出來: 示例 15.8?[http_fetch.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_15/http_fetch.go): ~~~ package main import ( "fmt" "io/ioutil" "log" "net/http" ) func main() { res, err := http.Get("http://www.google.com") checkError(err) data, err := ioutil.ReadAll(res.Body) checkError(err) fmt.Printf("Got: %q", string(data)) } func checkError(err error) { if err != nil { log.Fatalf("Get : %v", err) } } ~~~ 當訪問不存在的網站時,這里有一個`CheckError`輸出錯誤的例子: ~~~ 2011/09/30 11:24:15 Get: Get http://www.google.bex: dial tcp www.google.bex:80:GetHostByName: No such host is known. ~~~ *譯者注*?和上一個例子相似,你可以把google.com更換為一個國內可以順暢訪問的網址進行測試 在下邊的程序中,我們獲取一個twitter用戶的狀態,通過`xml`包將這個狀態解析成為一個結構: 示例 15.9?[twitter_status.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_15/twitter_status.go) ~~~ package main import ( "encoding/xml" "fmt" "net/http" ) /*這個結構會保存解析后的返回數據。 他們會形成有層級的XML,可以忽略一些無用的數據*/ type Status struct { Text string } type User struct { XMLName xml.Name Status Status } func main() { // 發起請求查詢推特Goodland用戶的狀態 response, _ := http.Get("http://twitter.com/users/Googland.xml") // 初始化XML返回值的結構 user := User{xml.Name{"", "user"}, Status{""}} // 將XML解析為我們的結構 xml.Unmarshal(response.Body, &user) fmt.Printf("status: %s", user.Status.Text) } ~~~ 輸出: ~~~ status: Robot cars invade California, on orders from Google: Google has been testing self-driving cars ... http://bit.ly/cbtpUN http://retwt.me/97p<exit code="0" msg="process exited normally"/> ~~~ 譯者注?和上邊的示例相似,你可能無法獲取到xml數據,另外由于go版本的更新,`xml.Unmarshal`函數的第一個參數需是[]byte類型,而無法傳入`Body`。 我們會在[章節15.4](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/15.4.md)中用到`http`包中的其他重要的函數: * `http.Redirect(w ResponseWriter, r *Request, url string, code int)`:這個函數會讓瀏覽器重定向到url(是請求的url的相對路徑)以及狀態碼。 * `http.NotFound(w ResponseWriter, r *Request)`:這個函數將返回網頁沒有找到,HTTP 404錯誤。 * `http.Error(w ResponseWriter, error string, code int)`:這個函數返回特定的錯誤信息和HTTP代碼。 * 另`http.Request`對象的一個重要屬性`req`:`req.Method`,這是一個包含`GET`或`POST`字符串,用來描述網頁是以何種方式被請求的。 go為所有的HTTP狀態碼定義了常量,比如: ~~~ http.StatusContinue = 100 http.StatusOK = 200 http.StatusFound = 302 http.StatusBadRequest = 400 http.StatusUnauthorized = 401 http.StatusForbidden = 403 http.StatusNotFound = 404 http.StatusInternalServerError = 500 ~~~ 你可以使用`w.header().Set("Content-Type", "../..")設置頭信息 比如在網頁應用發送html字符串的時候,在輸出之前執行`w.Header().Set("Content-Type", "text/html")`。 練習 15.4:擴展 http_fetch.go 使之可以從控制臺讀取url,使用[章節12.1](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/12.1.md)學到的接收控制臺輸入的方法 ([http_fetch2.go](https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/examples/chapter_15/http_fetch2.go)) 練習 15.5:獲取json格式的推特狀態,就像示例 15.9(twitter_status_json.go)
                  <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>

                              哎呀哎呀视频在线观看