Skip to content

Instantly share code, notes, and snippets.

@atoonk
Created March 11, 2024 20:15
Show Gist options
  • Save atoonk/2ed96f863523875b87da3deb1b7143d9 to your computer and use it in GitHub Desktop.
Save atoonk/2ed96f863523875b87da3deb1b7143d9 to your computer and use it in GitHub Desktop.
net.dial http get
package main
import (
"bufio"
"fmt"
"io"
"net"
)
func main() {
// Dial the server
conn, err := net.Dial("tcp", "1.1.1.1:80")
if err != nil {
fmt.Println("Error dialing:", err)
return
}
defer conn.Close()
// Send a GET request
_, err = conn.Write([]byte("GET / HTTP/1.1\r\nHost: 1.1.1.1\r\n\r\n"))
if err != nil {
fmt.Println("Error sending request:", err)
return
}
// Read and print the response
reader := bufio.NewReader(conn)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err != io.EOF {
fmt.Println("Error reading response:", err)
}
break
}
fmt.Print(line)
}
}
@atoonk
Copy link
Author

atoonk commented Mar 11, 2024

This will connect to 1.1.1.1 port 80
do a GET request and print the reply (301 redirect)

$ go run main.go
HTTP/1.1 301 Moved Permanently
Server: cloudflare
Date: Mon, 11 Mar 2024 20:15:36 GMT
Content-Type: text/html
Content-Length: 167
Connection: keep-alive
Location: https://1.1.1.1/
CF-RAY: 862e37af9bb5842f-YVR

<html>
<head><title>301 Moved Permanently</title></head>
<body>
<center><h1>301 Moved Permanently</h1></center>
<hr><center>cloudflare</center>
</body>
</html>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment