[GO]HTTP 请求实例(GET)以及设置代理

package controllers

import (
	"fmt"
	"github.com/astaxie/beego"
	"io/ioutil"
	"net/http"
)

type MainController struct {
	beego.Controller
}

func (c *MainController) Get() {
	c.TplNames = "index.html"
}

func (this *MainController) Test() {
//注释取消要需引入包net/url 通过设置代理请求下载页面
        //url, _ := url.Parse("172.17.184.82")
	//proxy := http.ProxyURL(url)
	//client := &http.Client{
	//	Transport: &http.Transport{
	//		Proxy: proxy,
	//	},
	//}
	resp, err := http.Get("http://www.baidu.com/")
	if err != nil {
		fmt.Println(err)
	}
	defer resp.Body.Close()
	body, err := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
	this.Ctx.WriteString(string(body))
}

你可能感兴趣的:(GO语言)