I've been working on simple http server in java. When I try to send some http request via google
browser "line" variable should have request string but it has junk characters.
Do you have any idea what is causing the problem?
Code is from this tutorial: https://www.youtube.com/watch?v=FqufxoA4m70&t=2061s&ab_channel=BloomInstituteofTechnology
Here is part of code where you can find this problem.
package com.company;
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class Main
{
public static void main(String[] args) throws Exception
{
try( ServerSocket socketServer = new ServerSocket(8080))
{
System.out.println("Server created");
while (true)
{
try (Socket client = socketServer.accept())
{
System.out.println("Client connected: " client.toString());
InputStreamReader isr = new InputStreamReader(client.getInputStream());
BufferedReader br = new BufferedReader(isr);
StringBuilder request = new StringBuilder();
String line;
line = br.readLine();
while(!line.isBlank())
{
request.append(line);
line = br.readLine();
}
}
}
}
}
CodePudding user response:
I don't know what you're actually doing with your browser. What are you typing in the URL bar? If you are trying to use https instead of http it will not work. Maybe "google browser" is tricking you and doing stuff behind your back... not exactly sending simple HTTP gets.
This works. It's not perfect but at least all IO streams are properly closed and a dummy response is sent to the client (which otherwise will remain hanging).
Try it by typing in your browser http://localhost:8080/some-random-url.
I cannot try with chrome as I don't use it, but this works using Firefox. As a rule, i would test stuff like this with curl
and not with a browser - you have more control while testing simple stuff.
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
public class Main {
public static void main(String[] args) throws Exception {
try (ServerSocket socketServer = new ServerSocket(8080)) {
System.out.println("Server created");
while (true) {
try (Socket client = socketServer.accept()) {
System.out.println("Client connected: " client.toString());
try (final InputStream is = client.getInputStream();
final InputStreamReader isr = new InputStreamReader(is);
final BufferedReader br = new BufferedReader(isr);
final OutputStream os = client.getOutputStream()) {
final StringBuilder request = new StringBuilder();
String line;
while (!(line = br.readLine()).isBlank()) {
request.append(line).append("\n");
}
System.out.println(" REQUEST ");
System.out.println(request);
String response = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/plain\r\n"
"Connection: closed\r\n"
"\r\n"
"Hello there!\r\n";
os.write(response.getBytes(StandardCharsets.UTF_8));
os.flush();
System.out.println("----------------------- RESPONSE ");
System.out.println(response);
}
}
}
}
}
}
Random remark: don't implement an HTTP server on your own. Just do it to understand how it works.