go语言判断http响应包中是否包含特定字符串

在使用http探测网站的过程中,需要判断响应内容是否有预期内容。如是乎根据网上内容,利用go编写了一个探测脚本样例

package main

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

func main()  {
    
    
	//创建http客户端
	client:=http.Client{
    
    }
	//创建http请求内容
	request,_:=http.NewRequest("GET","https://www.example.com",nil)
	//设置UA头
	request.Header.Add("User-Agent","ABCDEDFFGG")
	response,_:=client.Do(request)
	//将响应内容读取到byte切片中
	str_byte,_:=ioutil.ReadAll(response.Body)
	//将切片转换为字符串
	str:=string(str_byte)
	//判断字符串中是否包含特定字符
	
	fmt.Println("是否包含:",strings.Contains(str,"test"))
	//关闭连接
	defer response.Body.Close()

猜你喜欢

转载自blog.csdn.net/Nicky_Zheng/article/details/107867028