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
}
?>
An http server understand words like GET, POST, PUT, DELETE. So here's a problem:
outToServer.writeBytes(sentence + "\n");
outToServer.writeChars("GET /echo.php HTTP/1.1" +"\n");
The first statement there is telling something to the http server that it doesn't understand: the sentence. Comment out that line and your request becomes valid, and you will get a response.
I guess you want to post the sentence as a parameter to the echo service. You can put it in the query string:
outToServer.writeChars("GET /echo.php?message=" + sentence + " HTTP/1.0\n\n");
I also changed the HTTP version and appended an extra \n as pointed out by the comment of Steffen Ullrich:
Also you must add empty line to mark the end of the header. And the line ending must be \r\n not \n. And you should better do a HTTP/1.0 request not HTTP/1.1 since your code is neither able to deal with HTTP keep-alive nor with HTTP chunked encoding
However, this is not quite enough. You also need to encode some characters, such as spaces. The URL in the get request must be encoded, as pointed out by #zapl in a comment.
I also recommend to test your echo service first using simple telnet:
telnet localhost 8000
There, you can type your message, "GET /echo.php?message=something" and verify it works. Once you find something that works as intended, you can update the Java code accordingly.
Related
I have a client application which send POST request(json) to a custom server. The server must send a response(json) to the incoming message, but i haven't detected any response on the client side.
The problem is not on the client's side, because if it sends a request to another server, then after a few seconds it receives a response and I see it in the logs.
SERVER CODE
try{
server = new ServerSocket(4321);
client = server.accept();
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
System.out.println("Connection received from " + client.getInetAddress());
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
String s = "SERVER: Started.";
Gson gson = new Gson();
String json = gson.toJson(jsonObject.toString());
while ((s = in.readLine()) != null) {
System.out.println("RECV: "+s);
ss = s.split("PUSH\\s");
out.println("HTTP/1.1 200 OK");
out.println("application/json;charset=UTF-8");
out.println("application/json;charset=UTF-8");
out.println("Jersey/2.27 (HttpUrlConnection 1.8.0_291)");
out.println("no-cache");
out.println("no-cache");
out.println("hostname:4321");
out.println("keep-alive");
out.println("392");
out.println("\n");
out.println(json);
} catch(Exception e) {e.printStackTrace();}
I think the root of my issue is out.println(). I don't know exactly what the server should send back to client.
Response must contain json!
Also, i don't have the client code.
Could you help?
While I definitively wouldn't recommend writing an HTTP server this way there are at least two problems in your code:
You are missing header names, e.g. application/json;charset=UTF-8 should read Content-Type: application/json;charset=UTF-8
out.println() uses the line separator string as defined by the system property line.separator (e.g. \n for Linux). HTTP on the other hand needs \r\n, so better write it like this: out.print("HTTP/1.1 200 OK\r\n");
Try this:
out.print("HTTP/1.1 200 OK" + "\r\n");
out.print("Content-Type: application/json" + "\r\n");
// you shouldn't need the other headers…
out.print("\r\n");
out.print(json);
I have been playing around with UPnP, to get an understanding of how it all works, before I try working with any of the APIs that are out there, or doing anything more substantial. I have been reading through the UPnP documentation, and have used that information to format the messages that I am sending. I am just working from the command line right now, and have gotten discovery messages to work without issue. Now, I'm trying to return content from a ContentDirectory Browse() request (I have also tried TransportAV GetMediaInfo() because it takes only one argument). However, no matter what I try, I am getting a Null response from the MediaServer.
public class SOAPSocket2 {
public static void main(String[] args) {
try {
String xmldata = "<?xml version=\"1.0\"?>" +
"<s:Envelope " +
"xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/ \"" +
"s:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">" +
"<s:Body>" +
"<u:GetMediaInfo xmlns:u=\"urn:schemas-upnp-org:service:AVTransport:1\">" +
"<InstanceID>0</InstanceID>" +
"</u:GetMediaInfo>" +
"</s:Body>" +
"</s:Envelope>";
//Create socket
String hostname = args[0];
int port = Integer.parseInt(args[1]);
Socket sock = new Socket(hostname, port);
//Send header
String path = args[2];
BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream(),"UTF-8"));
// You can use "UTF8" for compatibility with the Microsoft virtual machine.
wr.write("POST " + path + " HTTP/1.1\r\n");
wr.write("HOST: " + hostname + ":" + port +"\r\n");
wr.write("CONTENT-TYPE: text/xml; charset=\"utf-8\"\r\n");
wr.write("SOAPACTION: \"urn:schemas-upnp-org:service:AVTransport:1#GetMediaInfo\"");
wr.write("\r\n");
//Send data
wr.write(xmldata);
wr.flush();
// Response
BufferedReader rd = new BufferedReader(new InputStreamReader(sock.getInputStream()));
String line;
line = rd.readLine();
System.out.println(line);
while((line = rd.readLine()) != null)
System.out.println(line);
} catch (Exception e) {
e.printStackTrace();
}
}
}
I know this isn't the most proper code, but I borrowed it from here: http://users.skynet.be/pascalbotte/rcx-ws-doc/xmlpost.htm. I figured that if I could actually get some sort of data in a response, then I could work on building it properly. I have modified it so that I pass the IP address and Port of the Media Server from the command line, as well as the path to the Control URL. However, I am getting nothing but 'null' from the Media Server. Any thoughts on what I'm doing incorrectly? Thanks
I am getting a Null response from the MediaServer
Is that a response from MediaServer? I would imagine it's just BufferedReader telling you there's nothing to return.
You have two things here you can debug (sending and receiving) but the same tools should help with both. Use wireshark or another network traffic capture tool to see the actual data that goes through the network. Wireshark will tell you if the response is sent (but you are failing to receive it properly) or if the reply never comes (implying your message is incorrect). It will also show your message as it is on the wire, making it easier to notice mistakes.
Doing the above (and pasting the messages here if you can't figure it out) is the best way to continue debugging, but I can see some problems in the code already:
SOAPACTION-line is missing "\r\n" in the end
There is no CONTENT-LENGTH header (this is required in normal cases)
These aren't even UPnP problems really, the message just isn't proper HTTP. Still, the UPnP Device Architecture document will help with problems like this.
I'm recently learning how to create sockets to connect to a webserver. I've managed to write a little something in Java:
BufferedReader inUser = new BufferedReader(new
InputStreamReader(System.in));
Socket clientSocket = new Socket("www.google.com", 80); // url expected
DataOutputStream outServer = new DataOutputStream
(clientSocket.getOutputStream());
BufferedReader inServer = new BufferedReader(new
InputStreamReader(clientSocket.getInputStream()));
String sentence = inUser.readLine();
outServer.writeBytes(sentence + '\n');
String modifiedSentence = inServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
inUser.close();
outServer.close();
inServer.close();
clientSocket.close();
I'm also using a socketTest program (from http://sockettest.sourceforge.net/) to test my client. The connection seems fine and I can use the sockettest to receive and send back messages (by hosting a local server). When I try to send a string to a webserver (in my java code it's named 'sentence'), it returns bad requests for random input like 'sd' or 'a', as expected. However, when I type the query I wished to receive feedback on, I don't receive anything. To be sure, this is what I put in (stored in 'sentence'):
GET index.html http/1.0
Either I should get the file if it exists or an exception if something went wrong, right? I don't receive anything though. Stranger yet, I've noticed that the first time I give input, I just have to make sure I have 3 separate random strings (separated by space) to have it accepted as valid input. And any random input I enter afterwards, like 'sd' will also be accepted.
Another observation I made is that the program keeps running. Normally I should read a single line then the program stops. This means it wasn't able to read anything.
I'm using port 80 for all the pages I've tried. Here's a small list of websites I've tried to perform a query on:
- www.google.com
- en.wikipedia.org
- www.cracked.com
I've tried a few others setup for the sole purpose of tutorials. Why don't I receive anything? When I tried it with telnet some seemed to work (though www.google.com always returned a xxx error found).
Try writing an additional "\r\n" before flushing the output stream:
BufferedReader inUser = new BufferedReader(new InputStreamReader(System.in));
URL url = new URL("http://www.google.com");
Socket clientSocket = new Socket(url.getHost(), 80); // url expected
OutputStream output = clientSocket.getOutputStream();
PrintWriter pw = new PrintWriter(output,false);
pw.print("GET index.html HTTP/1.0\r\n");
pw.print("\r\n");
pw.flush();
BufferedReader input = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String modifiedSentence = input.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
I am able to send data to a sinatra server (Strings) but I'm not sure about how to actually receive it in the sinatra code. Could anyone help?
Java client (what i'm using to send data) code:
private static void contactServer() {
try {
String text = "This is a text please work";
Socket sock = new Socket("localhost", 4567);
OutputStream os = sock.getOutputStream();
URL url = new URL("http://localhost:4567/hello");
PrintWriter writer = new PrintWriter(os);
writer.flush();
writer.write(text);
url.openStream();
System.out.println("done");
String strTemp = "";
/*while(null != (strTemp = br.readLine())){
System.out.println(strTemp);
}*/
} catch (Exception e) {
e.printStackTrace();
}
}
That's not going to work. Sinatra's server understands and speaks a language called the http protocol, so sending the string:
"This is a text please work"
through a socket to a Sinatra app is a hopeless prayer.
A protocol is a set of rules that specify how the client and the server will speak to each other--then each party can understand exactly what other party is saying. For the http protocol, clients send something known as a request, and servers reply with something known as a response. The request and the response must be formatted precisely according to the rules specified by the http protocol. The gory details for a request are here:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
So your java program needs to send a request to Sinatra's server, which is simply a string formatted in a precise manner. Here is what a simple GET request looks like:
GET /path/to/page HTTP/1.1
Host: localhost:4567
If you want to make a GET request for the page:
http://localhost:4567/page1
(i.e. hit the Sinatra route get '/page1')
...then a simple GET request for that page would look like:
GET /page1 HTTP/1.1
Host: localhost:4567
Also, you must end every line in the http request with "\r\n" no matter what OS you are using. Those two characters are part of the http protocol. Furthermore, after the last header there must be a blank line signified by another "\r\n", like this:
GET /page1 HTTP/1.1\r\nHost: localhost:4567\r\n\r\n
Here is the java:
import java.io.*;
import java.net.*;
public class Sinatra {
private static void contactServer() {
try {
Socket sock = new Socket("localhost", 4567);
OutputStream os = sock.getOutputStream();
PrintWriter writer = new PrintWriter(os);
String[] text = {
"GET /page1 HTTP/1.1",
"Host: localhost:4567",
};
String request = "";
for(int i=0; i < text.length; ++i) {
request += text[i] + "\r\n";
}
request += "\r\n";
System.out.println(request);
writer.write(request);
writer.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Sinatra.contactServer();
}
}
Note: the first few times I ran that java program, the server(which I started with $ ruby myapp.rb) threw the following error:
[2013-08-19 20:10:11] ERROR Errno::ECONNRESET: Connection reset by peer
/Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:80:in `eof?'
/Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/httpserver.rb:80:in `run'
/Users/7stud/.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
But when I ran the java program a few more times, the server eventually behaved. I have no idea what is causing that error. When I entered the url in FireFox, the server never threw that error. So I used Firebug to look at the request that Firefox sends; then I used all the same request headers in the java program, but the server still threw that error.
Edit: I can get the server error to go away by making the java program sleep for 1 second before closing the socket. The socket closes when you explicitly close the socket or when the program ends. Without the sleep, I think the socket closes while the server is still processing the request. Because a browser keeps the socket open, a browser never causes the server to throw that error.
The same server error also occurs with a ruby client:
require 'socket'
port = 4567
host = 'localhost'
s = TCPSocket.new host, port
req = [
"GET /page1 HTTP/1.1",
"Host: localhost:4567",
"Accept: */*",
]
req = req.join("\r\n") << ("\r\n" * 2)
print req
s.write req
s.flush
#sleep(1)
s.close
And the fix is the same. The only niggling detail is why the curl unix command doesn't cause the server to throw that error:
$ curl -v http://localhost:4567/page1
* About to connect() to localhost port 4567 (#0)
* Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 4567 (#0)
> GET /page1 HTTP/1.1
> User-Agent: curl/7.19.7 (universal-apple-darwin10.0) libcurl/7.19.7 OpenSSL/0.9.8r zlib/1.2.3
> Host: localhost:4567
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: text/html;charset=utf-8
< Content-Length: 0
< X-Xss-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< X-Frame-Options: SAMEORIGIN
< Server: WEBrick/1.3.1 (Ruby/1.9.3/2012-04-20)
< Date: Tue, 20 Aug 2013 04:59:16 GMT
< Connection: Keep-Alive
<
* Connection #0 to host localhost left intact
* Closing connection #0
With the -v option, curl prints out the request and the response. Using curl to make the request, I never saw the sever throw that error. I wonder if curl does a sleep too?
Good evening to all of you
I want to fetch a webpage using the socket class in java and i have done this as
import java.net.*;
import java.io.*;
class htmlPageFetch{
public static void main(String[] args){
try{
Socket s = new Socket("127.0.0.1", 80);
DataInputStream dIn = new DataInputStream(s.getInputStream());
DataOutputStream dOut = new DataOutputStream(s.getOutputStream());
dOut.write("GET /index.php HTTP/1.0\n\n".getBytes());
boolean more_data = true;
String str;
while(more_data){
str = dIn.readLine();
if(str==null)
more_data = false;
System.out.println(str);
}
}catch(IOException e){
}
}
}
But it is just giving the null's.
Output
HTTP/1.1 302 Found
Date: Wed, 01 Dec 2010 13:49:02 GMT
Server: Apache/2.2.11 (Unix) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8k PHP/5.2.9 mod_apreq2-20051231/2.6.0 mod_perl/2.0.4 Perl/v5.10.0
X-Powered-By: PHP/5.2.9
Location: http://localhost/xampp/
Content-Length: 0
Content-Type: text/html
null
I'm not sure if this is causing your problem, but HTTP expects carriage return and line feed for a newline:
dOut.write("GET /index.php HTTP/1.0\r\n\r\n".getBytes());
Also, it wouldn't hurt to flush and close your DataOutputStream:
dOut.flush();
dOut.close();
If you plan on doing anything more with this code than just connecting to simple test cases, I'd recommending using HttpURLConnection for this instead of implenting HTTP in a socket yourself. Otherwise, the result will contain more than just the web page. It will also contain the HTTP response including status codes and headers. Your code would need to parse that.
Update:
Looking at the response you added, that 302 response along with the Location: header indicate that the page you are looking for moved to http://localhost/xampp/ (see HTTP 302) and there is no longer any content at the original URL. This is something that is can be set to be handled automatically by HttpURLConnection or another library like Apache HttpClient. You will need to parse the status code, parse the headers, open a new socket to the response Location and get the page. Depending upon the exact requirements of your assignment, you will probably want to familiarize yourself with the HTTP 1.0 Specification, and the HTTP 1.1 Specification as well.
I think the code is working, except maybe that you don't see the output because it's swamped by all the nulls you print. You should stop the while after the first null.
More generally, DataInputStream and DataOutputStream are not the right classes for this job. Try this code.
public static void main(String[] args) throws IOException {
Socket s = new Socket("127.0.0.1", 80);
BufferedReader dIn = new BufferedReader(new InputStreamReader(s.getInputStream()));
PrintStream dOut = new PrintStream(s.getOutputStream());
dOut.println("GET /index.php HTTP/1.0");
dOut.println();
String str = null;
do {
str = dIn.readLine();
System.out.println(str);
} while (str != null);
}
Why are you using socket directly to perform HTTP connection? This is fine exercise but it requires deep knowledge of internals of HTTP protocol. Why not just to use classes URL, and URLConnection?
BufferedReader dIn = new BufferedReader(new URL("http://127.0.0.1:80").openConnection().getInputStream());
do {
str = dIn.readLine();
System.out.println(str);
} while (str != null);
}