httpTest.go 970 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package main
  2. import (
  3. "io"
  4. "log"
  5. "net/http"
  6. "os"
  7. "github.com/cryptix/goSam"
  8. )
  9. func main() {
  10. //In order to enable debugging, pass the SetDebug(true) option.
  11. //sam, err := goSam.NewClientFromOptions(SetDebug(true))
  12. // create a default sam client
  13. sam, err := goSam.NewDefaultClient()
  14. checkErr(err)
  15. log.Println("Client Created")
  16. // create a transport that uses SAM to dial TCP Connections
  17. tr := &http.Transport{
  18. Dial: sam.Dial,
  19. }
  20. // create a client using this transport
  21. client := &http.Client{Transport: tr}
  22. // send a get request
  23. resp, err := client.Get("http://stats.i2p/")
  24. checkErr(err)
  25. defer resp.Body.Close()
  26. log.Printf("Get returned %+v\n", resp)
  27. // create a file for the response
  28. file, err := os.Create("stats.html")
  29. checkErr(err)
  30. defer file.Close()
  31. // copy the response to the file
  32. _, err = io.Copy(file, resp.Body)
  33. checkErr(err)
  34. log.Println("Done.")
  35. }
  36. func checkErr(err error) {
  37. if err != nil {
  38. log.Fatal(err)
  39. }
  40. }