fsockopen equivalent in golang


Example with http link:

import(
    "fmt"
    "net"
    "bytes"
)
func main() {
    conn, err := net.Dial("tcp", "google.com:80")
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    defer conn.Close()
    fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
    var buf bytes.Buffer
    io.Copy(&buf, conn)
    fmt.Println("Total size:", buf.Len())
}

Example with https link:

import(
    "fmt"
    "crypto/tls"
    "bytes"
)
func main() {
    conn, err := tls.Dial("tcp", "www.google.com:443", nil)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    defer conn.Close()
    fmt.Fprintf(conn, "GET / HTTP/1.0\r\n\r\n")
    var buf bytes.Buffer
    io.Copy(&buf, conn)
    fmt.Println("Total size:", buf.Len())
}

Leave a Reply