【GO】20.golang http请求实例

  • 通用方法

package http

import (
	"bytes"
	"gosports/common/consts"
	"io/ioutil"
	"net/http"
)

func Get(url string) ([]byte, error) {
	resp, err := http.Get(url)

	if err != nil{
		return nil, err
	}

	defer func(){
		_ = resp.Body.Close()
	}()

	respBody, err := ioutil.ReadAll(resp.Body)

	if err != nil{
		return nil, err
	}

	return respBody, nil
}

func Post(url string, body []byte) ([]byte, error) {
	resp, err := http.Post(url, consts.HeaderContentTypeJson, bytes.NewReader(body))

	if err != nil{
		return nil, err
	}

	defer func(){
		_ = resp.Body.Close()
	}()

	respBody, err := ioutil.ReadAll(resp.Body)

	if err != nil{
		return nil, err
	}
	return respBody, nil
}

func Request(url string, header map[string]string, reqType string, body []byte) ([]byte, error) {
	req, err := http.NewRequest(reqType, url, bytes.NewReader(body))

	if err != nil{
		return nil, err
	}

	for k, v := range header{
		req.Header.Add(k, v)
	}

	client := &http.Client{}
	resp, err := client.Do(req)

	if err != nil{
		return nil, err
	}

	defer func(){
		_ = resp.Body.Close()
	}()

	respBody, err := ioutil.ReadAll(resp.Body)

	if err != nil{
		return nil, err
	}

	return respBody, nil
}
  • 封装client结构

package http

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

type Client struct {
	client  *http.Client
	url     string
	reqType string
	header  map[string]string
	body    []byte
}

func (c *Client) Request() ([]byte, error) {
	req, err := http.NewRequest(c.reqType, c.url, bytes.NewReader(c.body))

	if err != nil{
		return nil, err
	}

	for k, v := range c.header{
		req.Header.Add(k, v)
	}

	resp, err := c.client.Do(req)

	if err != nil{
		return nil, err
	}

	defer func(){
		_ = resp.Body.Close()
	}()

	respBody, err := ioutil.ReadAll(resp.Body)

	if err != nil{
		return nil, err
	}

	return respBody, nil
}

func NewClient(url string, header map[string]string, reqType string, body []byte) *Client {
	client := &Client{
		client:  &http.Client{},
		url:     url,
		reqType: reqType,
		header:  header,
		body:    body,
	}

	return client
}
  • 调用

package main

import (
	"fmt"
	"gosports/common/consts"
	"gosports/lib/http"
)

func main(){
	url := "http://www.baidu.com"

	rep, err := http.Get(url)
	if err != nil{
		fmt.Printf("get url failed url: %s | err: %s \n", url, err)
	}else {
		fmt.Printf("get url seccess. content: %s \n", string(rep))
	}

	rep, err = http.Post(url, nil)
	if err != nil{
		fmt.Printf("post url failed url: %s | err: %s \n", url, err)
	}else {
		fmt.Printf("post url seccess. content: %s \n", string(rep))
	}

	header := make(map[string]string)
	header["Content-Type"] = "application/json"
	header["User-Agent"] = "***"
	body := "{\"phone_number\":\"01111122222\",\"password\":\"123456\"}"
	rep, err = http.Request("http://***/login", header, consts.HttpPost, []byte(body))
	if err != nil{
		fmt.Printf("request get url failed url: %s | err: %s \n", url, err)
	}else {
		fmt.Printf("request get url seccess. content: %s \n", string(rep))
	}

	client := http.NewClient("http://***/login", header, consts.HttpPost, []byte(body))
	rep, err = client.Request()
	if err != nil{
		fmt.Printf("request get url failed url: %s | err: %s \n", url, err)
	}else {
		fmt.Printf("request get url seccess. content: %s \n", string(rep))
	}
}
  • 补充

package consts

const (
	HttpGet     = "GET"
	HttpPost    = "POST"
	HttpPut     = "PUT"
	HttpDelete  = "DELETE"
	HttpTrace   = "TRACE"
	HttpHead    = "HEAD"
	HttpOptions = "OPTIONS"
	HttpConnect = "CONNECT"
)

const HeaderContentTypeJson = "application/json;charset=utf-8"

 

你可能感兴趣的:(Go技术积累)