hostclient.go 801 B

1234567891011121314151617181920212223242526272829303132333435
  1. package main
  2. import (
  3. "fmt"
  4. "os"
  5. "github.com/valyala/fasthttp"
  6. )
  7. func main() {
  8. // Get URI from a pool
  9. url := fasthttp.AcquireURI()
  10. url.Parse(nil, []byte("http://localhost:8080/"))
  11. url.SetUsername("Aladdin")
  12. url.SetPassword("Open Sesame")
  13. hc := &fasthttp.HostClient{
  14. Addr: "localhost:8080", // The host address and port must be set explicitly
  15. }
  16. req := fasthttp.AcquireRequest()
  17. req.SetURI(url) // copy url into request
  18. fasthttp.ReleaseURI(url) // now you may release the URI
  19. req.Header.SetMethod(fasthttp.MethodGet)
  20. resp := fasthttp.AcquireResponse()
  21. err := hc.Do(req, resp)
  22. fasthttp.ReleaseRequest(req)
  23. if err == nil {
  24. fmt.Printf("Response: %s\n", resp.Body())
  25. } else {
  26. fmt.Fprintf(os.Stderr, "Connection error: %v\n", err)
  27. }
  28. fasthttp.ReleaseResponse(resp)
  29. }