-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTP_ProxyGet.go
72 lines (58 loc) · 1.28 KB
/
HTTP_ProxyGet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*
HTTP_ProxyGet
通过代理发起http请求
运行 (代码ip在网上搜索):
go run HTTP_ProxyGet.go http://139.59.81.158 http://google.com
*/
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"os"
)
func main() {
if len(os.Args) != 3 {
fmt.Println("Usage: ", os.Args[0], "http://proxy-host:port http://host:port/page")
os.Exit(1)
}
// 解析代理url
proxyString := os.Args[1]
proxyURL, err := url.Parse(proxyString)
checkError(err)
// 解析目标url
rawURL := os.Args[2]
url, err := url.Parse(rawURL)
checkError(err)
// 构建带proxy的client
transport := &http.Transport{Proxy: http.ProxyURL(proxyURL)}
client := &http.Client{Transport: transport}
request, err := http.NewRequest("GET", url.String(), nil)
dump, _ := httputil.DumpRequest(request, false)
fmt.Println(string(dump))
response, err := client.Do(request)
checkError(err)
fmt.Println("Read ok")
if response.Status != "200 OK" {
fmt.Println(response.Status)
os.Exit(2)
}
fmt.Println("Response ok")
var buf [512]byte
reader := response.Body
for {
n, err := reader.Read(buf[0:])
fmt.Println(string(buf[0:n]))
if err != nil {
os.Exit(0)
}
}
os.Exit(0)
}
func checkError(err error) {
if err != nil {
fmt.Println("Fatal error ", err.Error())
os.Exit(1)
}
}