I am hosting a simple PHP echo server locally. I am trying to send a message to the server in Java, and use a GET request to print the response but am getting a 'malformed HTTP request' error. Can anyone tell me how to correctly format the GET request?
//Client code:
import java.io.*;
import java.net.*;
public class TCPclient {
public static void main(String argv[]) throws Exception {
String sentence, modifiedSentence;
BufferedReader inFromUser = new BufferedReader(new InputStreamReader(System.in));
Socket clientSocket = new Socket("localhost", 8000);
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
sentence = inFromUser.readLine();
outToServer.writeBytes(sentence + "\n");
outToServer.writeChars("GET /echo.php HTTP/1.1" +"\n");
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
inFromServer.close();
outToServer.close();
inFromUser.close();
clientSocket.close();
}
}
//PHP Server code:
<?php
/* Simple php echo page
*/
// ini_set('display_errors', 'On');
// error_reporting(E_ALL | E_STRICT);
if(isset($_GET['source'])) {
if ($_GET['source'] == "raw")
echo file_get_contents(basename($_SERVER['PHP_SELF']));
else
echo "<pre>" . htmlspecialchars(file_get_contents(basename($_SERVER['PHP_SELF']))) . "</pre>";
} else if (isset($_GET['message'])){
echo strtoupper( htmlspecialchars($_GET['message'])) . '\n';
} else {
?>
<html>
<head>
<title>Error: No input message</title>
</head>
<body>
<h1> No message</h1>
<p>Echo server called without sending parameter</p>
</body>
</html>
<?php
}
?>