• go http.Get(), http.Head() io/ioutil ioutil.ReadAll(res.Body) 和 http.Get()


    http.Get() https://golang.org/pkg/net/http/#Client

    package main
    
    import (
    "fmt"
    "io/ioutil"
    "net/http"
    )
    func main() {
    	res, err := http.Get("https://www.baidu.com")
    	if err != nil {
    		fmt.Println("get err:", err)
    		return
    	}
    	data, err := ioutil.ReadAll(res.Body)
    	if err != nil {
    		fmt.Println("get data err:", err)
    		return
    	}
    
    	fmt.Println(string(data))
    }
    
    
    
    

    http.Head()

    package main
    
    import (
    	"fmt"
    	"net/http"
    )
    
    var url = []string {
    	"http://www.baidu.com",
    	"http://google.com",
    	"http://taobao.com",
    }
    func main(){
    	for _,v := range url{
    		resp, err := http.Head(v)
    		if err != nil {
    			fmt.Printf("head %s failed, err:%v \n", v, err)
    			continue
    			}
    		fmt.Printf("head succ, status:%v", resp.Status)
    	}
    }
    

    http.Head 升级,设置超时时间 Transport 是http网络传输传输底层的的一些东西,此处设置超时时间

    package main
    
    import (
    	"fmt"
    	"net"
    	"net/http"
    	"time"
    )
    
    var url = []string {
    	"http://www.baidu.com",
    	"http://google.com",
    	"http://taobao.com",
    }
    
    //Transport  是http网络传输传输底层的的一些东西,此处设置超时时间
    func main(){
    	for _, v := range url{
    	c := http.Client{  //
    		Transport: &http.Transport{
    			Dial: func(network, addr string) (net.Conn, error){
    				timeout := time.Second * 2
    				return net.DialTimeout(network, addr, timeout)
    			},
    		},
    	}
    	resp, err := c.Head(v)
    	if err != nil {
    		fmt.Printf("head %s failed, err:%v \n", v, err)
    		continue
    		}
    	fmt.Printf("head succ, status:%v", resp.Status)
    	}
    }
    

    http常用状态码

    http.StatusContinue=100
    http.StatusOK=200
    http.StatusFound =302
    http.StatusBadRequest=400
    http.StatusUnauthorized =401
    http.StatusForbidden =403
    http.StatusNotFound = 404
    http.StatusInternalServerError = 500
    
  • 相关阅读:
    poj 1321 棋盘问题 (dfs)
    poj 3274 Gold Balanced Lineup(哈希 )
    poj 2513 Colored Sticks( 字典树哈希+ 欧拉回路 + 并查集)
    zoj 3351 Bloodsucker(概率 dp)
    poj 1840 Eqs (hash)
    poj 2418 Hardwood Species (map)
    poj 2151 Check the difficulty of problems(概率dp)
    poj 2442 Sequence(优先队列)
    poj 1442 Black Box(堆 优先队列)
    两个STL网址 总结的很好 && c++堆的网址
  • 原文地址:https://www.cnblogs.com/heris/p/16032765.html
Copyright © 2020-2023  润新知