github.com/gogf/gf@v1.16.9/net/ghttp/ghttp_z_example_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package ghttp_test 8 9 import ( 10 "fmt" 11 "github.com/gogf/gf/frame/g" 12 "github.com/gogf/gf/net/ghttp" 13 "github.com/gogf/gf/os/gfile" 14 "time" 15 ) 16 17 func ExampleHelloWorld() { 18 s := g.Server() 19 s.BindHandler("/", func(r *ghttp.Request) { 20 r.Response.Write("hello world") 21 }) 22 s.SetPort(8999) 23 s.Run() 24 } 25 26 // Custom saving file name. 27 func ExampleUploadFile_Save() { 28 s := g.Server() 29 s.BindHandler("/upload", func(r *ghttp.Request) { 30 file := r.GetUploadFile("TestFile") 31 if file == nil { 32 r.Response.Write("empty file") 33 return 34 } 35 file.Filename = "MyCustomFileName.txt" 36 fileName, err := file.Save(gfile.TempDir()) 37 if err != nil { 38 r.Response.Write(err) 39 return 40 } 41 r.Response.Write(fileName) 42 }) 43 s.SetPort(8999) 44 s.Run() 45 } 46 47 func ExampleClientResponse_RawDump() { 48 response, err := g.Client().Get("https://goframe.org") 49 if err != nil { 50 panic(err) 51 } 52 response.RawDump() 53 } 54 55 // ExampleClient_SetProxy a example for `ghttp.Client.SetProxy` method. 56 // please prepare two proxy server before running this example. 57 // http proxy server listening on `127.0.0.1:1081` 58 // socks5 proxy server listening on `127.0.0.1:1080` 59 func ExampleClient_SetProxy() { 60 // connect to a http proxy server 61 client := g.Client() 62 client.SetProxy("http://127.0.0.1:1081") 63 client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout 64 response, err := client.Get("https://api.ip.sb/ip") 65 if err != nil { 66 // err is not nil when your proxy server is down. 67 // eg. Get "https://api.ip.sb/ip": proxyconnect tcp: dial tcp 127.0.0.1:1087: connect: connection refused 68 fmt.Println(err) 69 } 70 response.RawDump() 71 // connect to a http proxy server which needs auth 72 client.SetProxy("http://user:password:127.0.0.1:1081") 73 client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout 74 response, err = client.Get("https://api.ip.sb/ip") 75 if err != nil { 76 // err is not nil when your proxy server is down. 77 // eg. Get "https://api.ip.sb/ip": proxyconnect tcp: dial tcp 127.0.0.1:1087: connect: connection refused 78 fmt.Println(err) 79 } 80 response.RawDump() 81 82 // connect to a socks5 proxy server 83 client.SetProxy("socks5://127.0.0.1:1080") 84 client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout 85 response, err = client.Get("https://api.ip.sb/ip") 86 if err != nil { 87 // err is not nil when your proxy server is down. 88 // eg. Get "https://api.ip.sb/ip": socks connect tcp 127.0.0.1:1087->api.ip.sb:443: dial tcp 127.0.0.1:1087: connect: connection refused 89 fmt.Println(err) 90 } 91 fmt.Println(response.RawResponse()) 92 93 // connect to a socks5 proxy server which needs auth 94 client.SetProxy("socks5://user:password@127.0.0.1:1080") 95 client.SetTimeout(5 * time.Second) // it's suggested to set http client timeout 96 response, err = client.Get("https://api.ip.sb/ip") 97 if err != nil { 98 // err is not nil when your proxy server is down. 99 // eg. Get "https://api.ip.sb/ip": socks connect tcp 127.0.0.1:1087->api.ip.sb:443: dial tcp 127.0.0.1:1087: connect: connection refused 100 fmt.Println(err) 101 } 102 fmt.Println(response.RawResponse()) 103 } 104 105 // ExampleClientChain_Proxy a chain version of example for `ghttp.Client.Proxy` method. 106 // please prepare two proxy server before running this example. 107 // http proxy server listening on `127.0.0.1:1081` 108 // socks5 proxy server listening on `127.0.0.1:1080` 109 // for more details, please refer to ExampleClient_SetProxy 110 func ExampleClientChain_Proxy() { 111 client := g.Client() 112 response, err := client.Proxy("http://127.0.0.1:1081").Get("https://api.ip.sb/ip") 113 if err != nil { 114 // err is not nil when your proxy server is down. 115 // eg. Get "https://api.ip.sb/ip": proxyconnect tcp: dial tcp 127.0.0.1:1087: connect: connection refused 116 fmt.Println(err) 117 } 118 fmt.Println(response.RawResponse()) 119 120 client2 := g.Client() 121 response, err = client2.Proxy("socks5://127.0.0.1:1080").Get("https://api.ip.sb/ip") 122 if err != nil { 123 // err is not nil when your proxy server is down. 124 // eg. Get "https://api.ip.sb/ip": socks connect tcp 127.0.0.1:1087->api.ip.sb:443: dial tcp 127.0.0.1:1087: connect: connection refused 125 fmt.Println(err) 126 } 127 fmt.Println(response.RawResponse()) 128 }