package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
/**
如果我们自己开发的server程序,在Client请求时需要获取第三方网站
上数据并将其返回,
*/
func main(){
r := gin.Default()
r.GET("/GetOtherData", func(c *gin.Context) {
//url := "http://www.baidu.com"
url :="https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1485012388,2380514454&fm=26&gp=0.jpg"
response,err := http.Get(url)
if err != nil|| response.StatusCode != http.StatusOK{
c.Status(http.StatusServiceUnavailable) // 应答client
return
}
body := response.Body
contentLength := response.ContentLength
contentType := response.Header.Get("Content-Type")
// 数据写入响应体
c.DataFromReader(http.StatusOK,contentLength,contentType,body,nil)
})
r.Run(":9090")
}