zoukankan      html  css  js  c++  java
  • Go语言中的HTTP

    Go中的http使用

    package main
    
    import (
    	"fmt"
    	"net/http"
    	"io/ioutil"
    	"strings"
    	"net/url"
    )
    
    func httpGet(){
    	resp, err := http.Get("http://www.baidu.com")
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	defer resp.Body.Close()
    	body, err := ioutil.ReadAll(resp.Body)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	fmt.Println(string(body))
    }
    
    func httpPost(){
    	resp, err := http.Post("http://www.baidu.com",
    "application/x-www-form-urlencoded",
    strings.NewReader("name=zzr"))
    if err != nil {
    	fmt.Println(err)
    	return
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
    	fmt.Println(err)
    	return
    }
    fmt.Println(string(body))
    }
    
    func httpPostForm(){
    	resp, err := http.PostForm("http://www.baidu.com",
    	url.Values{"key":{"value"}, "id":{"123"}})
    
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	defer resp.Body.Close()
    	body, err := ioutil.ReadAll(resp.Body)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	fmt.Println(string(body))
    }
    
    func httpDo(){
    	client := &http.Client{}
    
    	req, err := http.NewRequest("POST", "http://www.baidu.com",strings.NewReader("name=zzr"))
    	if err != nil{
    		fmt.Println(err)
    		return
    	}
    	req.Header.Set("Content-Type", "application/x-www.form-urlencoded")
    	req.Header.Set("Cookie", "name=ben")
    
    	resp, err := client.Do(req)
    
    	defer resp.Body.Close()
    
    	body, err := ioutil.ReadAll(resp.Body)
    	if err != nil {
    		fmt.Println(err)
    		return
    	}
    	fmt.Println(string(body))
    }
    func main(){
    	httpPost()	
    }
    
  • 相关阅读:
    Clustering by fast search and find of density peaks
    《第一行代码》(二)
    TF-IDF
    《第一行代码》(一)
    《OpenCV入门》(三)
    OpenCV入门(二)
    协方差矩阵特征向量的意义
    ICA
    整数划分
    1144. The Missing Number (20)
  • 原文地址:https://www.cnblogs.com/zzr-stdio/p/9671647.html
Copyright © 2011-2022 走看看