1

I'm making a Java program which makes http get request and downloads a html file.Here is my code

  import java.io.*;
import java.net.*;

public class HTTP {

public static void main(String[] args) {
    try {
        FileOutputStream to_file = new FileOutputStream("f:\\temp.txt");
        URL url = new URL("http://www.dailygames.com/3dgames.html");

        String host = url.getHost();
        int port = url.getPort();
        if (port == -1) {
            port = 80;
        }

        String filename = url.getFile();
        System.out.println(filename);

        //Create Connection
        //Open socket to a specific host and port
        Socket socket = new Socket(host, port);

        //Get input and output streams for the socket
        InputStream from_server = socket.getInputStream();
        OutputStream outputStream = socket.getOutputStream();

        // Get response OR Instantiates a new PrintWriter passing in the sockets output stream
        PrintWriter to_server = new PrintWriter(outputStream);

        // Send headers OR Prints the request string to the output stream
        to_server.println("GET " + filename + " HTTP/1.1"); // This is a message sent to the server
        to_server.println("Host: " + host + "\r\n"); // As you can see, the domain name is passed to the program as an argument. After connected to the server, it sends the domain name, and reads the response from the server.
        to_server.flush();

        byte[] buffer = new byte[4096];
        int byte_read;

        //Reads & Prints each line of the response OR Reads HTTP response
        while ((byte_read = from_server.read(buffer)) != -1) {
            // Print server's response
            to_file.write(buffer, 0, byte_read);
            System.out.print((char) byte_read);
        }
        socket.close();
        to_file.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
}

When I run this code It creates a text file but when I open it the response is not the html file but a 400 error response.How can I solve this problem

enter image description here

Stark
  • 43
  • 5
  • 1
    Does this answer your question? [Why am I getting Http/1.1 400 Bad request?](https://stackoverflow.com/questions/5533791/why-am-i-getting-http-1-1-400-bad-request) – fantaghirocco Feb 24 '21 at 09:08
  • Sorry but it doesn't help me – Stark Feb 24 '21 at 09:11
  • 1
    are you sure that println adds "\r\n" and not "\n" on the stream? I would use a simple print and add the required "\r\n" by myself to be more sure of that (platform independent). Also I would talk in HTTP/1.0 and not HTTP/1.1 because your code would not be able to decode a chunked response from the server (and in HTTP/1.1 the response could be sent as chunks), but th'ats not yoyr current error 400 problem. – regilero Feb 24 '21 at 10:30
  • I assume that the point of this is to implement HTTP yourself, otherwise use an http client. – tgdavies Feb 25 '21 at 02:57
  • Yes I need to implement it in Java without using any HTTP Client – Stark Feb 26 '21 at 12:57
  • Could this problem be related to any system security settings which prevent sending http request through terminal in Mac – Stark Feb 26 '21 at 12:58
  • HttpUrlConnection is available in Java, and is way better than this code. Java 11 provides java.net.http.HttpClient which is even better. – Eric Pabst Apr 21 '23 at 03:53

0 Answers0