-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTP_Get.go
67 lines (56 loc) · 1.19 KB
/
HTTP_Get.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
/*
HTTP_Get
本代码用于获取网站http response的header
原始代码中判断utf8编码的方法有缺陷,因为没有处理大小写问题。html中文档编码并不限制大写或者小写。
运行
go run HTTP_Get.go http://www.golang.com/
*/
package main
import (
"fmt"
"net/http"
"net/http/httputil"
"os"
"strings"
)
func main() {
if len(os.Args) != 2 {
fmt.Println("Usage: ", os.Args[0], " host:port")
os.Exit(1)
}
url := os.Args[1]
response, err := http.Get(url)
if err != nil {
fmt.Println(err.Error())
os.Exit(2)
}
if response.Status != "200 OK" {
fmt.Println(response.Status)
os.Exit(3)
}
b, _ := httputil.DumpResponse(response, false)
fmt.Println(string(b))
contentTypes := response.Header["Content-Type"]
if !acceptableCharset(contentTypes) {
fmt.Println("Cannot handle", contentTypes)
os.Exit(4)
}
var buf [512]byte
reader := response.Body
for {
n, err := reader.Read(buf[0:])
fmt.Print(string(buf[0:n]))
if err != nil {
os.Exit(0)
}
}
os.Exit(0)
}
func acceptableCharset(contentTypes []string) bool {
for _, cType := range contentTypes {
if strings.Index(strings.ToLower(cType), "utf-8") != -1 {
return true
}
}
return false
}