I am trying to have a simple java server respond to an XmlHttpRequest, but I am not sure how. As of now, the socket on the server gets the request of the browser. After getting the sockets output stream, the server writes to it, then closes all connections. Is there something more to this? I assume that you would need a header to respond to the request correctly, if so what would that header be. Thanks!
Here is what I am doing right now. I can’t seem to get the browser to accept the message, however.
Edit:
The Java server code:
private String HEADER = "HTTP/1.1 200 OK\n" +
"Cache-Control: private\n" +
"Content-Type: text/html; charset=utf-8\n" +
"Expires: Sun, 21 Feb 2100 20:39:08 GMT\n" +
"Server: Microsoft-IIS/7.5\n" +
"Date: Sun, 21 Apr 2012 20:39:07 GMT\n" +
"Connection: close\n" +
"Content-Length: 10\n";
browserWriter =
new PrintWriter(
browser.getOutputStream(),true);
while((data = browserReader.readLine()) != null) {
if (data.indexOf("POST") != -1) {
/* TODO TEST - TAKE OUT */
browserWriter.println(HEADER + "APPLESAUSE");
browserWriter.close();
browserReader.close();
browser.close();
return;
Javascript:
xmlhttp.open("POST","http://10.0.2.15:1024/password?url=" + site, false);
xmlhttp.send(null);
All XmlHTTPRequest’s require an HTTP response, so HTTP headers are needed. It’s ok to close the connection once the response is given. This is basically just acting as a web server.
Some HTTP header fields: http://en.wikipedia.org/wiki/List_of_HTTP_header_fields
See here for the official spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
A decent tutorial here: http://net.tutsplus.com/tutorials/other/http-headers-for-dummies/