test engine
test engine

Reputation: 13

Cant read the whole response from the HttpURLConnection I want to achieve that for My Proxy Server?

    public void run()
    {
        try
        {
            BufferedReader inFromClient = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
            DataOutputStream outForClient = new DataOutputStream(clientSocket.getOutputStream());

            String request = inFromClient.readLine();

            String[] requestParts = request.split(" ");
            String method = requestParts[0]; // Extract request method
            String url = requestParts[1];

            logger.log(Level.INFO, "Outbound {0} request :: {1}", new Object[]{method, url});

            URL destinationUrl = new URL(url);
            HttpURLConnection connection = (HttpURLConnection) destinationUrl.openConnection(Proxy.NO_PROXY);
            connection.setRequestMethod(method); // Set the original request method

            // Forward headers from the client to the destination server
            for (String headerLine = inFromClient.readLine(); headerLine != null && !headerLine.isEmpty(); headerLine = inFromClient.readLine())
            {
                String[] headerParts = headerLine.split(": ", 2);
                connection.setRequestProperty(headerParts[0], headerParts[1]);
            }

            connection.setRequestProperty("Proxy-Status","ADSMSecurity");
            // Send the request body if present
            if (method.equals("POST"))
            {
                connection.setDoOutput(true);
                DataOutputStream outToServer = new DataOutputStream(connection.getOutputStream());
                outToServer.writeBytes("\r\n");
                outToServer.flush();
                outToServer.close();
            }

            connection.connect();
            BufferedReader inputBuffer = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
            String response = inputBuffer.lines().collect(Collectors.joining("\n"));
            logger.log(Level.INFO,"Response :: {0}",response);
            inFromClient.close();
            outForClient.close();
            clientSocket.close();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }

How can I read the entire response as a stream from the HTTPURLConnection instead of just the response body? The connection.getInputStream() method only provides the response body of the HTTPURLConnection request to the server. Additionally, I'm interested in any external java components that can achieve this. Burp Suite accomplishes this through its proxy functionality in java , so if they can do it, there must be a way. Please help me find it. I am avoiding reconstructing the response as it could potentially introduce security vulnerabilities such as injection attacks. Therefore, I aim to provide the response exactly as received by the client from the server.

HTTP/2 400 Bad Request
Vary: Origin
Vary: X-Origin
Vary: Referer
Content-Type: application/json; charset=UTF-8
Date: Sun, 17 Mar 2024 05:44:14 GMT
Server: ESF
Cache-Control: private
Content-Length: 561
X-Xss-Protection: 0
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Alt-Svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000

Error

Upvotes: 1

Views: 22

Answers (0)

Related Questions