日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

golang中net/http包的簡單使用

 印度阿三17 2019-06-27

一、介紹

http包提供了http客戶端和服務(wù)端的實現(xiàn)

Get,Head,Post和PostForm函數(shù)發(fā)出http、https的請求

程序在使用完回復(fù)后必須關(guān)閉回復(fù)的主體

#簡單的訪問網(wǎng)站,由于沒有添加header,訪問數(shù)據(jù)不正確
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    res,err:=http.Get("https://www.baidu.com")
    if err !=nil{
        panic(err)
    }
    defer res.Body.Close()
    body, err := ioutil.ReadAll(res.Body)
    fmt.Println(string(body))
}
#建立的client客戶端,發(fā)起的get請求,在請求體中添加header,然后使用client執(zhí)行這個請求
package main

import (
    "fmt"
    "io/ioutil"
    "net/http"
)

func main() {
    client:=&http.Client{}
    res,err:=http.NewRequest("GET","https://www.baidu.com",nil)
    res.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36")
    resp,err:=client.Do(res)
    if err != nil {
        panic(err)
    }
    defer resp.Body.Close()
    body, err := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))
}

二、其他使用方法

#監(jiān)聽ip端口,并建立一個sever,讓訪問者可以訪問指定的文件目錄
package main

import "net/http"

func main() {
  //設(shè)置訪問路由 http.Handle("/",http.FileServer(http.Dir("./"))) http.ListenAndServe(":8080",nil) }
package main

import (
    "bufio"
    "fmt"
    "github.com/axgle/mahonia"
    "golang.org/x/net/html/charset"
    "golang.org/x/text/encoding"
    "io"
    "io/ioutil"
    "net/http"
    "strconv"
)

func main() {
    client := &http.Client{}
    request, err := http.NewRequest("GET", "http://www.baidu.com", nil)
    if err != nil {
        fmt.Println(err)
    }
    //建立cookie對象
    cookie := &http.Cookie{Name: "maple", Value: strconv.Itoa(123)}
    request.AddCookie(cookie) //向request中添加cookie

    //設(shè)置request的header
    request.Header.Set("Accept", "text/html,application/xhtml xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3")
    request.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
    request.Header.Set("Cache-Control", "no-cache")
    request.Header.Set("Connection", "keep-alive")

    response, err := client.Do(request)
    if err != nil {
        fmt.Println(err)
        return
    }
    e:=determineEncoding(response.Body)
    fmt.Println("當(dāng)前編碼:",e)

    defer response.Body.Close()
    if response.StatusCode == 200 {
        r, err := ioutil.ReadAll(response.Body)
        if err != nil {
            fmt.Println(err)
        }


        //編碼轉(zhuǎn)換,如編碼不正確可轉(zhuǎn)成指定編碼
        srcCoder :=mahonia.NewEncoder("utf-8")
        res:=srcCoder.ConvertString(string(r))

        fmt.Println(res)
    }
}

//編碼檢測
func determineEncoding(r io.Reader) encoding.Encoding  {
    bytes, err := bufio.NewReader(r).Peek(1024)
    if err != nil {
        panic(err)
    }
    e, _, _ := charset.DetermineEncoding(bytes, "")
    return e
}

?

來源:https://www./content-4-273201.html

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多