Answer the question
In order to leave comments, you need to log in
Why is the web server not responding?
Started learning http protocol. In theory, after sending the headers, the server should at least return something. But as usual, everything is in my ass :D
package main
import "net"
import "fmt"
import "bufio"
import "log"
func main() {
conn, err := net.Dial("tcp", "127.0.0.1:80")
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(conn, "GET /adm/ HTTP/1.1")
fmt.Fprintf(conn, "Host: localhost")
fmt.Fprintf(conn, "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:38.0) Gecko/20100101 Firefox/38.0")
fmt.Fprintf(conn, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8")
fmt.Fprintf(conn, "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3")
fmt.Fprintf(conn, "Connection: close")
for {
message, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Fatal(err)
}
fmt.Print("Message from server: " + message)
}
}
Answer the question
In order to leave comments, you need to log in
Add \r\n to the end of each line, and after the entire query, do it again. Otherwise, the server will not understand how to separate the headers among themselves and how to determine the end of the request.
package main
import (
"bufio"
"fmt"
"log"
"net"
)
func main() {
conn, err := net.Dial("tcp", "ya.ru:80")
if err != nil {
log.Fatal(err)
}
fmt.Fprintf(conn, "GET / HTTP/1.1\r\n")
fmt.Fprintf(conn, "Host: localhost\r\n")
fmt.Fprintf(conn, "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:38.0) Gecko/20100101 Firefox/38.0\r\n")
fmt.Fprintf(conn, "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n")
fmt.Fprintf(conn, "Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.5,en;q=0.3\r\n")
fmt.Fprintf(conn, "Connection: close\r\n")
fmt.Fprintf(conn, "\r\n")
for {
message, err := bufio.NewReader(conn).ReadString('\n')
if err != nil {
log.Fatal(err)
}
fmt.Print("Message from server: " + message)
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question