I'm trying to write data to /Users/brichards/message.html, but I'm unable to do so. I suspect the error may be involving the header of args[] (which is the host). I know the packet will need the host as the header, but I keep getting an error. Here is what I have so far:
public static void SocketandURL(String[] args) throws Exception {
String host = args[0];
// Build and send a GET request packet. The packet is a String,
// and therefore needs to be converted to a sequence of bytes before it
// can be sent. There's a getBytes() method in the String class to do that.
// The \r and \n characters are CR and LF. The first pair ends the GET line,
// the second pair ends the "blank line" marking the end of the packet.
// Any header info could have to be included before that blank line.
String packet = "GET /wireshark-labs/INTRO-wireshark-file1.html HTTP/1.1\r\n\r\n";
packet = "Host: " + host + packet;
System.out.println("Sending packet to " + host);
try {
// Read the resulting data and write it to our output file. We intentionally
// use a DataInputStream rather than an InputStreamReader to get the data from
// the socket, since we want to allow for reading binary data files like .jpgs,
// but that means we need to convert it to a character string if we ever want
// to print it out. This code uses a special constructor in the String class
// to do that. Note that this code assumes the entire response can be retrieved
// in one call to read(). You may NOT make that assumption in your finished
// program.
//Get DataInputStream of Array
theSocket = new Socket(host, 80); // Port 80 is HTTP
byte[] buf = new byte[BUFFER_SIZE];//New byte array
outgoing = theSocket.getOutputStream();
incoming = new DataInputStream(theSocket.getInputStream());
outgoing.write(packet.getBytes());
int numBytes = incoming.read(buf);
String filename = "textfile.txt";
File theFile = new File(filename);
if (args.length > 0) {
filename = args[0];
}
if (theFile.exists()) {
System.out.println("It already exists. Contents will be overwritten");
} else {
System.out.println("Creating new file: " + theFile.getAbsolutePath());
FileMake(args);
}
FileOutputStream writer = new FileOutputStream(theFile);
String filePath = "/Users/bakya/Downloads/HW2/textfile.txt";
writer.write(filePath.getBytes());
writer.close();
while (numBytes > 0) {
System.out.println("Got: " + (new String(buf, 0, numBytes, "UTF-8")));
host += args[0];
numBytes = incoming.read(buf);
}
theSocket.close();
} catch (Exception e) {
System.out.println("ERR: Invalid File");
}
}
I want to print
Grabbing /wireshark-labs/INTRO-wireshark-file1.html from gaia.cs.umass.edu
Writing data to /Users/brichards/message.html
But my error says:
Sending packet to gaia.cs.umass.edu
It already exists. Contents will be overwritten
Got: HTTP/1.1 400 Bad Request
Date: Mon, 25 Sep 2023 00:46:07 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.33 mod_perl/2.0.11 Perl/v5.16.3
Content-Length: 226
Connection: close
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>400 Bad Request</title>
</head><body>
<h1>Bad Request</h1>
<p>Your browser sent a request that this server could not understand.<br />
</p>
</body></html>
Is there a way to get what I want to print?
It seems that this line is adding the
Hostheader before the first line of an HTTP request (theGETin this case). If that intends to be valid HTTP the order should be inverted andpacketshould be first.