Send httprequest in Java using java.net package - java

This is my code on the client side:
import java.io.*;
import java.net.*;
public class httpClient {
public void TcpSocket()
{
String sentence;
String modifiedSentence;
StringBuffer contents= null;
//open a socket connection on port 80
try{
Socket clientSocket = new Socket("localhost", 8080);
//send message to the server
DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
//read message from the server
BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
//read http request message from a file
File file = new File("/home/x/Desktop/test.txt");
contents = new StringBuffer();
BufferedReader reader = null;
reader = new BufferedReader(new FileReader(file));
String text = null;
// repeat until all lines is read
while ((text = reader.readLine()) != null)
{
contents.append(text).append(System.getProperty("line.separator"));
}
//end reading file
//Send message
sentence = contents.toString();
outToServer.writeBytes(sentence + '\n');
modifiedSentence = inFromServer.readLine();
System.out.println("FROM SERVER: " + modifiedSentence);
clientSocket.close();
}
catch (UnknownHostException e) {
System.err.println("Don't know about host");
System.exit(1);
} catch (IOException e) {
System.err.println("Couldn't get I/O for the connection");
System.exit(1);
}
}
public static void main(String args[])
{
httpClient cl = new httpClient();
cl.TcpSocket();
}
}
and my http server:
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Executors;
import com.sun.net.httpserver.Headers;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;
public class HttpServerDemo {
public static void main(String[] args) throws IOException {
InetSocketAddress addr = new InetSocketAddress(8080);
HttpServer server = HttpServer.create(addr, 0);
server.createContext("/", new MyHandler());
server.setExecutor(Executors.newCachedThreadPool());
server.start();
System.out.println("Server is listening on port 8080" );
}
}
class MyHandler implements HttpHandler {
public void handle(HttpExchange exchange) throws IOException {
System.out.println("I am in Server request Handler"); <---------------Request is not coming here
String requestMethod = exchange.getRequestMethod();
if (requestMethod.equalsIgnoreCase("GET")) {
Headers responseHeaders = exchange.getResponseHeaders();
responseHeaders.set("Content-Type", "text/plain");
exchange.sendResponseHeaders(200, 0);
OutputStream responseBody = exchange.getResponseBody();
Headers requestHeaders = exchange.getRequestHeaders();
Set<String> keySet = requestHeaders.keySet();
Iterator<String> iter = keySet.iterator();
while (iter.hasNext()) {
String key = iter.next();
List values = requestHeaders.get(key);
String s = key + " = " + values.toString() + "\n";
responseBody.write(s.getBytes());
}
responseBody.close();
}
}
}
Request I am sending:
GET /index.html HTTP/1.1
Host: localhost
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3
Accept: /
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
But my request is not coming in to HttpServerDemo.java's request handler.
Update
I am not able to debug my code since it's not hitting the request handler on the server and it works fine in a real browser. This is the response from the real browser when I open http://localhost:8080
Host = [localhost:8080]
Accept-charset = [ISO-8859-1,utf-8;q=0.7,*;q=0.7]
Accept-encoding = [gzip,deflate]
Connection = [keep-alive]
Keep-alive = [300]
Accept-language = [en-us,en;q=0.5]
User-agent = [Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.1.3) Gecko/20091020 Ubuntu/9.10 (karmic) Firefox/3.5.3]
Accept = [text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8]

You forgot a blank newline between request headers and request body. The server now thinks that there are more headers to come and is waiting with handling the response. You must always insert a blank newline (CRLF) after the request headers. See also Wikipedia: HTTP.

Why use a Socket for HTTP at all? All this stuff is already solved for you with URL.openConnection().

Related

Send request body with GET request using HttpURLConnection in java

Kindly don't confuse my question with sending body with POST request using HttpURLConnection.
I want to send body with GET request using HttpURLConnection. Here is code i am using.
public static String makeGETRequest(String endpoint, String encodedBody) {
String responseJSON = null;
URL url;
HttpURLConnection connection;
try {
url = new URL(endpoint);
connection = (HttpURLConnection) url.openConnection();
connection.setInstanceFollowRedirects(true);
connection.setDoOutput(true);
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.connect();
OutputStream outputStream = connection.getOutputStream();
outputStream.write(encodedBody.getBytes());
outputStream.flush();
Util.log(connection,connection.getResponseCode()+":"+connection.getRequestMethod());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String responseChunk = null;
responseJSON = "";
while ((responseChunk = bufferedReader.readLine()) != null) {
responseJSON += responseChunk;
}
bufferedReader.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
Util.log(e, e.getMessage());
}
return responseJSON;
}
what happens is that the request type is identified automatically depending on the connection.getInputStream() and connection.getOutPutStream().
when you call connection.getOutPutStream() the request type is automatically set to POST even if you have explicitly set request type to GET using connection.setRequestMethod("GET").
The problem is that i am using 3rd party Web Service(API) which accepts request parameters as body with GET request
<get-request>
/myAPIEndPoint
body = parameter1=value as application/x-www-form-urlencoded
<response>
{json}
I am well aware that most of the case GET don't have request body but many of the web service often uses GET request with parameters as body instead of query string. Kindly guide me how i can send GET request with body in android without using any 3rd party library(OkHttp,Retrofit,Glide etc)
use this code you will need to do a little modification but it will get the job done.
package com.kundan.test;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.Socket;
public class GetWithBody {
public static final String TYPE = "GET ";
public static final String HTTP_VERSION = " HTTP/1.1";
public static final String LINE_END = "\r\n";
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost", 8080); // hostname and port default is 80
OutputStream outputStream = socket.getOutputStream();
outputStream.write((TYPE + "<Resource Address>" + HTTP_VERSION + LINE_END).getBytes());//
outputStream.write(("User-Agent: Java Socket" + LINE_END).getBytes());
outputStream.write(("Content-Type: application/x-www-form-urlencoded" + LINE_END).getBytes());
outputStream.write(LINE_END.getBytes()); //end of headers
outputStream.write(("parameter1=value&parameter2=value2" + LINE_END).getBytes()); //body
outputStream.flush();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
StringBuilder builder = new StringBuilder();
String read = null;
while ((read = bufferedReader.readLine()) != null) {
builder.append(read);
}
String result = builder.toString();
System.out.println(result);
}
}
this the Raw HTTP Request Dump
GET <Resource Address> HTTP/1.1
User-Agent: Java Socket
Content-Type: application/x-www-form-urlencoded
parameter1=value&parameter2=value2
Note : This is for http request if you want https Connection Please refer to the link SSLSocketClient

How to get query string from Socket?

I'm coding web-server based on sockets. So I can get HTTP request headers:
import java.net.ServerSocket;
import java.net.Socket;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class HttpServer {
public static void main(String[] args) throws Throwable {
//http://localhost:3000
ServerSocket ss = new ServerSocket(3000);
while (true) {
//Waiting for socket
Socket s = ss.accept();
System.out.println("Client accepted");
//The main process
new SocketProcessor(s,ss).start();
}
}
private static class SocketProcessor implements Runnable {
private Thread t;
private Socket s;
private InputStream is;
private OutputStream os;
private SocketProcessor(Socket s,ServerSocket ss) throws Throwable {
t = new Thread(this, "Server Thread");
this.s = s;
this.is = s.getInputStream();
this.os = s.getOutputStream();
}
public void run() {
try {
readInputHeaders();
writeResponse("<html><body><h1>Hello</h1></body></html>");
} catch (Throwable t) {
/*do nothing*/
} finally {
try {
s.close();
} catch (Throwable t) {
}
}
System.out.println("Client processing finished");
}
public void start()
{
t.start();
}
private void writeResponse(String s) throws Throwable {
String response = "HTTP/1.1 200 OK\r\n" +
"Server: Server\r\n" +
"Content-Type: text/html\r\n" +
"Content-Length: " + s.length() + "\r\n" +
"Connection: close\r\n\r\n";
String result = response + s;
os.write(result.getBytes());
os.flush();
}
private void readInputHeaders() throws Throwable {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while(true) {
String s = br.readLine();
System.out.println(s);
if(s == null || s.trim().length() == 0) {
break;
}
}
}
}
}
Input:
http://localhost:3000/?page=1
Output:
GET /?page=1 HTTP/1.1
Host: localhost:3000
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: ru-RU,ru;q=0.8,en-US;q=0.6,en;q=0.4,bg;q=0.2
But now I need to get query string params:
page=1
I know that GET-request is not the best example, because I can get params currently from URI, but this will not work with POST.
So how can I get query string params from socket? I have no idea to try.
Of course I've found a solution. POST parameters land after a new line. So checking if line is empty doesn't help. We could check Content-Length and read request char by char.

Why am I getting a HTTP 400 bad request response when sending a manually-crafted HTTP request over a TCP socket?

I'm trying to build an HTTP request manually (as a string) and send it over a TCP socket, this is what I'm trying to do:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* SimpleHttpClient.java (UTF-8)
*
* Mar 27, 2014
*
* #author tarrsalah.org
*/
public class SimpleHttpClient {
private static final String StackOverflow = "http://stackoverflow.com/";
public static void main(String[] args) {
// if (args.length < 1) {
// System.out.println("Usage : SimpleHttpClient <url>");
// return;
// }
try {
URL url = new URL(StackOverflow);
String host = url.getHost();
String path = url.getPath();
int port = url.getPort();
if (port < 80) {
port = 80;
}
//Construct and send the HTTP request
String request = "GET" + path + "HTTP/1.1\n";
request += "host: " + host;
request += "\n\n";
// Open a TCP connection
Socket socket = new Socket(host, port);
// Send the request over the socket
PrintWriter writer = new PrintWriter(socket.getOutputStream());
writer.print(request);
writer.flush();
// Read the response
BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String next_record = null;
while ((next_record = reader.readLine()) != null) {
System.out.println(next_record);
}
socket.close();
} catch (MalformedURLException ex) {
Logger.getLogger(SimpleHttpClient.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(SimpleHttpClient.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
But I'm getting this response message whatever the URL is chosen:
HTTP/1.0 400 Bad request
Cache-Control: no-cache
Connection: close
Content-Type: text/html
<html><body><h1>400 Bad request</h1>
Your browser sent an invalid request.
</body></html>
Why ?
Thanks to Julian Reschke for the suggestion , I was missing tow space characters (One after the HTTP verb and the second after the path)
String request = "GET " + path + " HTTP/1.1\n";
// ^ ^
I'm actually a bit unsure whether this solves your problem, but if you read RFC2616, section 5 you will note that it very specifically mentions CRLF's after Request-line and headers, so you might be missing some \r's in your code.
Cheers,

connecting to google host and return nothing

I was wrote fowling code then set HTTP proxy on 127.0.0.1:9090 and try to reach google.com but it print following output and nothing happend.
Output:
Waiting for clients on port 9090
Got connection from /127.0.0.1:11827
Active Connections = 1
Waiting for clients on port 9090
connect started
connect finished
***Got connection from /74.125.232.131:80
writed
GET http://google.com/ HTTP/1.1
Host: google.com
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate
Cookie: ****
Connection: keep-alive
null
read line startes
Code:
import java.net.*;
import java.io.*;
import java.util.*;
public class SocketGoogler
{
public static void main(String[] args)
{
int port = 9090;
try {
ServerSocket server = new ServerSocket(port);
//System.out.println(google());
while(true) {
System.out.println("Waiting for clients on port " + port);
Socket client = server.accept();
ConnectionHandler handler = new ConnectionHandler(client);
handler.start();
}
} catch(Exception ex) {
System.out.println("Connection error: "+ex);
}
}
}
class ConnectionHandler extends Thread {
private Socket client;
BufferedReader reader;
PrintWriter writer;
static int count;
public ConnectionHandler(Socket client) {
this.client = client;
System.out.println("Got connection from "+client.getInetAddress()
+":"+client.getPort());
count++;
System.out.println("Active Connections = " + count);
}
public void run() {
String message;
String totalMessage;
try {
reader = new BufferedReader(new
InputStreamReader(client.getInputStream()));
writer = new PrintWriter(client.getOutputStream());
writer.flush();
message = reader.readLine();
totalMessage=message+"\n";
while (message != null) {
message = reader.readLine();
totalMessage+=message+"\n";
}
client.close();
//System.out.println(totalMessage);
google(totalMessage);
count--;
System.out.println("Active Connections = " + count);
} catch (Exception ex) {
count--;
System.out.println("Active Connections = " + count);
}
}
public String google(String w) throws IOException
{
String message , totalMessage;
int port = 80;
//ServerSocket server = new ServerSocket(port,10,InetAddress.getByName("google.com"));
//Socket googler=server.accept();
Socket googler=new Socket();
InetSocketAddress endpoint=new InetSocketAddress(InetAddress.getByName("google.com").getHostAddress(), port);
System.out.println("connect started");
googler.connect(endpoint);
System.out.println("connect finished");
System.out.println("***Got connection from "+googler.getInetAddress()+":"+googler.getPort());
BufferedReader reader = new BufferedReader(new
InputStreamReader(googler.getInputStream()));
PrintWriter writer=new PrintWriter(googler.getOutputStream());
//String w="GET http://google.com/ HTTP/1.1\nHost: google.com\nUser-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\nAccept-Language: en-US,en;q=0.5\nAccept-Encoding: gzip, deflate\nConnection: keep-alive"+null;
writer.print(w);
System.out.println("writed\n"+w);
System.out.println("read line startes");
message = reader.readLine();
System.out.println("read line finished");
totalMessage=message+"\n";
while (message != null) {
message = reader.readLine();
System.out.println("*");
totalMessage+=message+"\n";
}
googler.close();
System.out.println("close");
return totalMessage;
}
}
Why this problems happens?I already connected too google.com and send request but nothing respond from this host.
Use writer.flush(); It will send your request.
:)

Java Server: Socket sending HTML code to browser

I am trying to write a simple Java program using ServerSockets that will send some HTML code to the browser. Here is my code:
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(55555);
} catch (IOException e) {
System.err.println("Could not listen on port: 55555.");
System.exit(1);
}
Socket clientSocket = null;
try {
clientSocket = serverSocket.accept();
if(clientSocket != null) {
System.out.println("Connected");
}
} catch (IOException e) {
System.err.println("Accept failed.");
System.exit(1);
}
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
out.println("HTTP/1.1 200 OK");
out.println("Content-Type: text/html");
out.println("\r\n");
out.println("<p> Hello world </p>");
out.flush();
out.close();
clientSocket.close();
serverSocket.close();
I then go to localhost:55555 in my browser and nothing displays. I know the connection is working because the program outputs "Connected" as checked in the if statement. I have also tried outputting the data from the inputStream and that works. But the text I am trying to output in the browser is not displaying at all, the program finishes running and I get a
"Problem loading page - the connection has been reset"
in my browser, but no text.
I have searched the internet and it seems everyone else coding it this way is having their text display fine, they are having other problems.
How can I fix this?
I tested your code in Chrome, Firefox, IE, and Opera and it works.
However, I would suggest that you use multi-threading and essentially spawn a new thread to handle each new request.
You can create a class that implements runnable and takes a clientSocket within the constructor. This will essentially make your custom webserver capable of accepting more than one request concurrently.
You will also need a while loop if you want to handle more than one total requests.
A good read demonstrating the above: https://web.archive.org/web/20130525092305/http://www.prasannatech.net/2008/10/simple-http-server-java.html
If the web-archive is not working, I'm posting the code below (taken from above):
/*
* myHTTPServer.java
* Author: S.Prasanna
* #version 1.00
*/
import java.io.*;
import java.net.*;
import java.util.*;
public class myHTTPServer extends Thread {
static final String HTML_START =
"<html>" +
"<title>HTTP Server in java</title>" +
"<body>";
static final String HTML_END =
"</body>" +
"</html>";
Socket connectedClient = null;
BufferedReader inFromClient = null;
DataOutputStream outToClient = null;
public myHTTPServer(Socket client) {
connectedClient = client;
}
public void run() {
try {
System.out.println( "The Client "+
connectedClient.getInetAddress() + ":" + connectedClient.getPort() + " is connected");
inFromClient = new BufferedReader(new InputStreamReader (connectedClient.getInputStream()));
outToClient = new DataOutputStream(connectedClient.getOutputStream());
String requestString = inFromClient.readLine();
String headerLine = requestString;
StringTokenizer tokenizer = new StringTokenizer(headerLine);
String httpMethod = tokenizer.nextToken();
String httpQueryString = tokenizer.nextToken();
StringBuffer responseBuffer = new StringBuffer();
responseBuffer.append("<b> This is the HTTP Server Home Page.... </b><BR>");
responseBuffer.append("The HTTP Client request is ....<BR>");
System.out.println("The HTTP request string is ....");
while (inFromClient.ready())
{
// Read the HTTP complete HTTP Query
responseBuffer.append(requestString + "<BR>");
System.out.println(requestString);
requestString = inFromClient.readLine();
}
if (httpMethod.equals("GET")) {
if (httpQueryString.equals("/")) {
// The default home page
sendResponse(200, responseBuffer.toString(), false);
} else {
//This is interpreted as a file name
String fileName = httpQueryString.replaceFirst("/", "");
fileName = URLDecoder.decode(fileName);
if (new File(fileName).isFile()){
sendResponse(200, fileName, true);
}
else {
sendResponse(404, "<b>The Requested resource not found ...." +
"Usage: http://127.0.0.1:5000 or http://127.0.0.1:5000/</b>", false);
}
}
}
else sendResponse(404, "<b>The Requested resource not found ...." +
"Usage: http://127.0.0.1:5000 or http://127.0.0.1:5000/</b>", false);
} catch (Exception e) {
e.printStackTrace();
}
}
public void sendResponse (int statusCode, String responseString, boolean isFile) throws Exception {
String statusLine = null;
String serverdetails = "Server: Java HTTPServer";
String contentLengthLine = null;
String fileName = null;
String contentTypeLine = "Content-Type: text/html" + "\r\n";
FileInputStream fin = null;
if (statusCode == 200)
statusLine = "HTTP/1.1 200 OK" + "\r\n";
else
statusLine = "HTTP/1.1 404 Not Found" + "\r\n";
if (isFile) {
fileName = responseString;
fin = new FileInputStream(fileName);
contentLengthLine = "Content-Length: " + Integer.toString(fin.available()) + "\r\n";
if (!fileName.endsWith(".htm") && !fileName.endsWith(".html"))
contentTypeLine = "Content-Type: \r\n";
}
else {
responseString = myHTTPServer.HTML_START + responseString + myHTTPServer.HTML_END;
contentLengthLine = "Content-Length: " + responseString.length() + "\r\n";
}
outToClient.writeBytes(statusLine);
outToClient.writeBytes(serverdetails);
outToClient.writeBytes(contentTypeLine);
outToClient.writeBytes(contentLengthLine);
outToClient.writeBytes("Connection: close\r\n");
outToClient.writeBytes("\r\n");
if (isFile) sendFile(fin, outToClient);
else outToClient.writeBytes(responseString);
outToClient.close();
}
public void sendFile (FileInputStream fin, DataOutputStream out) throws Exception {
byte[] buffer = new byte[1024] ;
int bytesRead;
while ((bytesRead = fin.read(buffer)) != -1 ) {
out.write(buffer, 0, bytesRead);
}
fin.close();
}
public static void main (String args[]) throws Exception {
ServerSocket Server = new ServerSocket (5000, 10, InetAddress.getByName("127.0.0.1"));
System.out.println ("TCPServer Waiting for client on port 5000");
while(true) {
Socket connected = Server.accept();
(new myHTTPServer(connected)).start();
}
}
}
Enjoy!
You need to need to set the PrintWriter to autoflush when it prints.
PrintWriter out = new PrintWriter(clientSocket.getOutputStream());
should be
PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
The line terminator in HTTP is \r\n. This means that you shouldn't use println(), you should use print() and add an explicit \r\n yourself to each line.
The result of an HTTP GET is supposed to be an HTML document, not a fragment. Browsers are entitled to ignore or complain. Send this:
<html>
<head/>
<body>
<p> Hello world </p>
</body>
</html>
in my computer, at least to get the socket's inputStream:
clientSocket.getInputStream();
with out this line, sometimes chrome doesn't work
you need read the input from socket first
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String line = "";
while((line = bufferedReader.readLine()) != null){
System.out.println(line);
if(line.isEmpty())
break;
}
You should accept the request sent by client(browser in this case),to do that just add the following lines:
Buffered reader in = new Buffered reader(new InputStreamReader(client_socket.getInputStream()));
Note: you need to replace the "client_socket" part with name of your own socket for the client.
Why we need to accept browser request?
It's because if we don't accept the request browser doesn't get any acknowledgement from the server that the request that was sent is received,hence it thinks the server is not reachable any more.
My code:
public class Help {
public static void main(String args) throws IOException{
ServerSocket servsock new serverSocket(80)
Socket cs servsock, accept();
Printwriter out new Printwriter(Cs.getoutputstream), true)
BufferedReader in new BufferedReader(new InputStreamReader(cs.getInputStream());
out.println("<html> <body> <p>My first StackOverflow answer </p> </body> </html>");
out.close();
servsock.close();
}
}
You need to send
"HTTP/1.1 200 OK" first. followed by a newline
then "Content-Type: text/html; charset: UTF-8" followed by 2 newlines.
then send the HTML source for it to display as a styled webpage and not just a text output.
I used OutputStreamWriter
OutputStreamWriter osw = new OutputStreamWriter(socket.getOutputStream());
osw.write("HTTP/1.1 200 OK\nContent-Type: text/html; charset=UTF-8\n\n");
osw.write("<html><head><title>Hello World</title></head><body></body><p>Hello World</p></body></html>");
Not sending "HTTP/1.1 200 OK" first results in source code being displayed without html parsing..
And to avoid "Connection being reset" error, you need to close your socket. Very Important !!
The whole code:
import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
public class WebServer
{
ServerSocket serverSocket;
Socket socket;
FileReader fr;
OutputStreamWriter osw;
WebServer() throws IOException
{
serverSocket = new ServerSocket(8080);
socket = serverSocket.accept();
fr = new FileReader("index.html");
osw = new OutputStreamWriter(socket.getOutputStream());
osw.write("HTTP/1.1 200 OK\nContent-Type: text/html; charset=UTF-8\n\n");
int c;
char[] ch = new char[4096];
while ((c = fr.read(ch)) != -1)
{
osw.write(ch, 0, c);
}
osw.close();
socket.close();
}
public static void main(String[] args) throws IOException
{
new WebServer();
}
}

Categories