How to use HTTP long links (client side) in Golang

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Most of the restful requests can be connected with a short connection, that is, three handshake to establish a link, the exchange of data after the completion of the release of the link, the short link will not take longer to occupy the port number, the actual project will also use another, long links, such as the client sends a restful request, the need to monitor a resource change situation, The server provides a watch mechanism to notify the client when resources change.

So client side, relative to the short link, long links should be how to write?

As with short links, you only need to loop through the response returned by the server side .

package mainimport (        "fmt"        "io"        "log"        "net/http")func main() {        request, err := http.NewRequest("GET", "http://www.example.com/", nil)        if err != nil {                log.Fatal(err)        }        http_client := &http.Client{}        response, err := http_client.Do(request)        if err != nil {                log.Fatal(err)        }        buf := make([]byte, 4096) // any non zero value will do, try '1'.        for {                n, err := response.Body.Read(buf)                if n == 0 && err != nil { // simplified                        break                }                fmt.Printf("%s", buf[:n]) // no need to convert to string here        }        fmt.Println()}

Reference: Https://stackoverflow.com/questions/10152478/how-to-make-a-long-connection-with-http-client

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.