Skip to content

Instantly share code, notes, and snippets.

@border
Created January 29, 2011 13:30
Show Gist options
  • Save border/801826 to your computer and use it in GitHub Desktop.
Save border/801826 to your computer and use it in GitHub Desktop.
Http Get Example
package main
import (
"log"
"io/ioutil"
"http"
)
func main() {
res, _, err := http.Get("http://bbs.golang-china.org/")
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)
log.Println(string(body))
}
package main
import (
"log"
"http"
"net"
"io/ioutil"
)
func main() {
url, err := http.ParseURL("http://bbs.golang-china.org/")
if err != nil {
log.Fatal(err)
}
tcpConn, err := net.Dial("tcp", "", url.Host + ":80")
if err != nil {
log.Fatal(err)
}
clientConn := http.NewClientConn(tcpConn, nil)
var req http.Request
req.URL = url
req.Method = "GET"
req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/534.11 (KHTML, like Gecko) Chrome/9.0.570.0 Safari/534.11"
req.Header = map[string][]string{}
req.Header.Add("Connection", "keep-alive")
err = clientConn.Write(&req)
if err != nil {
log.Fatal(err)
}
resp, err := clientConn.Read(&req)
if err != nil {
log.Fatal(err)
}
defer resp.Body.Close()
log.Println("Http Response: " + resp.Status)
body, _ := ioutil.ReadAll(resp.Body)
log.Println(string(body))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment