Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Java in General
Search Coderanch
Advance search
Google search
Register / Login
Help coderanch get a
new server
by contributing to the
fundraiser
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Devaka Cooray
Liutauras Vilda
Sheriffs:
Jeanne Boyarsky
paul wheaton
Henry Wong
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Carey Brown
Mikalai Zaikin
Bartenders:
Lou Hamers
Piet Souris
Frits Walraven
Forum:
Java in General
display http header
varsha simla
Greenhorn
Posts: 8
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hello.i need help on how to display http headers in a browser using the concept of client - proxy - server in
java
.
i'm new to networking in java.thanks in advance
Stephan van Hulst
Saloon Keeper
Posts: 15703
367
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Hello Varsha, welcome to CodeRanch!
What kind of help do you need? Please take a look at
HowToAskQuestionsOnJavaRanch
varsha simla
Greenhorn
Posts: 8
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
hello Stephan.
i want help concerning the coding part.
client side
import java.io.BufferedReader; import java.io.DataOutputStream; import java.io.InputStreamReader; import java.net.Socket; import java.util.StringTokenizer; class Client extends Thread { public static void main(String argv[]) throws Exception { /*String sentence; int modifiedSentence;*/ final String HTML_START ="<html>" + "<title>HTTP Server in java</title>" +"<body>"; final String HTML_END ="</body>" + "</html>"; Socket connectedClient = null; BufferedReader inFromClient = null; DataOutputStream outToClient = null; System.out.println("Client-Side"); //System.out.println( "The Client "+ connectedClient.getInetAddress() + ":" + connectedClient.getPort() + " is connected"); BufferedReader inFromUser = new BufferedReader( new InputStreamReader(System.in)); Socket clientSocket = new Socket("localhost", 5555); DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream()); BufferedReader inFromServer = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String requestString = inFromUser.readLine(); System.out.println("test: " + requestString); outToServer.writeBytes(requestString + '\n'); // modifiedSentence = inFromServer.read(); // System.out.println("FROM PROXY: " + modifiedSentence); clientSocket.close(); } }
import java.io.*; import java.net.*; import java.util.*; public class ProxyServer { public static final int portNumber = 5555; public static void main(String[] args){ System.out.println("... Starting Proxy Server ..."); try { ServerSocket proxySocket = new ServerSocket(portNumber); while(true){ Socket clientSocket = proxySocket.accept(); // reading the request from client BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream())); String clientSentence = inFromClient.readLine(); StringTokenizer tokenizer = new StringTokenizer(clientSentence); String httpMethod = tokenizer.nextToken(); String httpQueryString = tokenizer.nextToken(); // host to connect to System.out.println("" + "\n... Connecting to host ..." ); 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(clientSentence + "<BR>"); System.out.println(clientSentence); clientSentence = inFromClient.readLine(); } handler a=new handler(); if (httpMethod.equals("GET")) { //send request to server if (httpQueryString.equals("/")) { // The default home page a.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()){ a.sendResponse(200, fileName, true); } else { a.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 a.sendResponse(404, "<b>The Requested resource not found ...." + "Usage: http://127.0.0.1:5000 or http://127.0.0.1:5000/</b>", false); //(new myHTTPServer(clientSocket)).start(); } } catch (Exception e) { e.printStackTrace(); } // forward the response from the proxy to the server Socket hostSocket = new Socket("127.0.0.1", 8080); DataOutputStream sos = new DataOutputStream(hostSocket.getOutputStream()); System.out.println("\n\tForwarding request to server"); sos.writeBytes(clientSentence+ '\n'); // receive response from the server to proxy System.out.println("\n\tReceiving request from server"); BufferedReader FromServer = new BufferedReader(new InputStreamReader(hostSocket.getInputStream())); //System.out.println("666666Accepting Connection\n\n"); String serverSentence = FromServer.readLine(); System.out.println("\n\tResponse from Server: " + serverSentence); // forward the response from the proxy to browser DataOutputStream ToClient = new DataOutputStream(clientSocket.getOutputStream()); ToClient.writeBytes(serverSentence+ '\n'); //closing all sockets hostSocket.close(); clientSocket.close(); System.out.println("\nEnd of communication"); } } catch (IOException e) { e.printStackTrace();*/ }{//} }} //}
i have been able to do that much so far by doing some research and now i'm stuck.please help me
varsha simla
Greenhorn
Posts: 8
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
import java.io.DataOutputStream; import java.io.FileInputStream; public class handler { 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"; } DataOutputStream outToClient = null; 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(); } }
Stephan van Hulst
Saloon Keeper
Posts: 15703
367
posted 12 years ago
1
Number of slices to send:
Optional 'thank-you' note:
Send
What are you stuck with? Can you give a detailed description of the problems you're running into?
varsha simla
Greenhorn
Posts: 8
posted 12 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
the line 26 for the class ProxyServer
the variable clientSentence---
cannot be resolved to a variable
i also want to have an overview what is done at a proxy server when i am sending a http get request
is my code logic good?
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
How to create an Image
ClassFormatError with an Applet?
ping a website
how to implement proxy server that preserves incoming request's IP address
Proxy By-Pass
We need your help - Coderanch server fundraiser
More...