Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A \"proxy server\" acts as an intermediary between clients (perhaps many at the

ID: 3843690 • Letter: A

Question

A "proxy server" acts as an intermediary between clients (perhaps many at the same time) and a server. That is, it takes any requests it receives from clients and forward them to the real server and, similarly, forward any responses it receives from the server to the real clients. With this in mind, complete the following HTTPProxyServer class: import java.io.*; import java.net.*; public HTTPProxyServer implements tunable {private ServerSocket; public HTTPProxyServer (String actualHost, int actualPort, int intermadiaryPort) {} public void run() {}}

Explanation / Answer

The required code is given as below. Please follow the comments to learn the code thoroughly and I hope you have prior knowledge to sockets to understand this code.

ProxyServer.java

Hence , this is the complete code for the class file of HTTPProxyServer class.

import java.net.*;

import java.io.*;
import java.util.*;

public class HTTPProxyServer extends Thread {
private Socket socks = null;
private static final int SIZE = 32768;
public HTTPProxyServer(Socket socks)

{
super("Server");
this.socks = socks;
}

public void run() {

try {
DataOutputStream dos = new DataOutputStream(socks.getOutputStream());
BufferedReader scan = new BufferedReader(new InputStreamReader(socks.getInputStream()));

String LineIp, LineOp;
int count = 0;
String calling = "";

while ((LineIp = scan.readLine()) != null) { // Request with the help of client
try {
StringTokenizer sttok = new StringTokenizer(LineIp);
sttok.nextToken();
} catch (Exception exc) {
break;
}

           //Find the URL
if (count == 0) {
String[] tokens = LineIp.split(" ");
calling = tokens[1];
System.out.println("Request for : " + calling);
}

count++;
}
//End Client Request


BufferedReader scan1 = null;
try {
  
URL url = new URL(calling);
URLConnection conn = url.openConnection();
conn.setDoInput(true);
  
conn.setDoOutput(false);
System.out.println("Enter Input: "+ conn.getContentType());
System.out.println("Enter the length: "+ conn.getContentLength());
System.out.println("User Communication: "+ conn.getAllowUserInteraction());
System.out.println("Encoding The Content: "+ conn.getContentEncoding());
System.out.println("Type Of Content: "+ conn.getContentType());

// Get the response
InputStream input = null;
HttpURLConnection http = (HttpURLConnection)conn;
if (conn.getContentLength() > 0)
               {
try {
input = conn.getInputStream();
scan1 = new BufferedReader(new InputStreamReader(input));
}
                   catch (IOException inputexc)
                   {
System.out.println("Exception Produced: " + inputexc);
}
}
byte bite[] = new byte[ SIZE ]; //Receiving response for server
int indices = input.read( bite, 0, SIZE );
while ( indices != -1 )
{
dos.write( bite, 0, indices );
indices = input.read( bite, 0, SIZE );
}
dos.flush();
}
//End sending Response to the Client          
           catch (Exception exc)
           {
System.err.println("Exception Generated: " + exc);
dos.writeBytes("");
}

if (scan1 != null) //Close all resources
           {
scan1.close();
}
if (dos != null) {
dos.close();
}
if (scan != null) {
scan.close();
}
if (socks != null) {
socks.close();
}

} catch (IOException exc) {
exc.printStackTrace();
}
}
}

Please rate the answer if it helps... Thankyou

Hope it helps...

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote