Get raw HTTP request message that would be sent from a HttpGet?

Wojtek

How do I get a full raw HTTP request message in Apache http components v4.3.5. I managed to get only the parsed response.

public String get(String uri) {
    try (CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response1 = httpclient.execute(new HttpGet(uri))) {
        HttpEntity entity = response1.getEntity();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        entity.writeTo(byteArrayOutputStream);
        return new String(byteArrayOutputStream.toByteArray());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

There is a similar answer but for the old 3.1 version of apache commons-http v3.1. How do I do it v4.3.5?

Wojtek

Got it working like this. Not most elegant, cos the stream is being copied to memory, but works for my use case.

public class TestHttpClient {
    public String get(String uri) {
        StreamRecorder streamRecorder = new StreamRecorder();
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", new ListeningSocketFactory(streamRecorder))
                .register("https", SSLConnectionSocketFactory.getSocketFactory())
                .build();
        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
        try (CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(connManager).build(); CloseableHttpResponse response1 = httpclient.execute(new HttpGet(uri))) {
            HttpEntity entity = response1.getEntity();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            entity.writeTo(byteArrayOutputStream);
            return new String(streamRecorder.getRecordedStreamAsCharArray());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String s = new TestHttpClient().get("http://onet.pl");
        System.out.println("---------------------\n" +
                "RESULT:\n" +
                "---------------------" +
                "\n" + s.substring(0, Math.min(s.length(), 200)));
    }
}

public class ListeningSocketFactory extends PlainConnectionSocketFactory {
    private final CopyInputStream.InputStreamListener streamListener;

    public ListeningSocketFactory(StreamRecorder streamListener) {
        this.streamListener = streamListener;
    }

    @Override
    public Socket createSocket(HttpContext context) throws IOException {
        return new SocketWithInputStreamListener(streamListener);
    }

}

class StreamRecorder implements CopyInputStream.InputStreamListener {
    private final List<Integer> streamCopy = new ArrayList<>(1000);

    @Override
    public synchronized void processByte(int readByte) {
        streamCopy.add(readByte);
    }

    public synchronized char[] getRecordedStreamAsCharArray() {
        char[] result = new char[streamCopy.size()];
        for (int i = 0; i < streamCopy.size(); i++) {
            result[i] = (char) i;
        }
        return result;
    }
}


public class CopyInputStream extends FilterInputStream {
    private final InputStreamListener streamListener;

    CopyInputStream(InputStream in, InputStreamListener streamListener) {
        super(in);
        this.streamListener = streamListener;
    }

    @Override
    public int read() throws IOException {
        int readByte = super.read();
        processByte(readByte);
        return readByte;
    }

    @Override
    public int read(byte[] buffer, int offset, int count) throws IOException {
        int readBytes = super.read(buffer, offset, count);
        processBytes(buffer, offset, readBytes);
        return readBytes;
    }

    private void processBytes(byte[] buffer, int offset, int readBytes) {
        for (int i = 0; i < readBytes; i++) {
            processByte(buffer[i + offset]);
        }
    }

    private void processByte(int readByte) {
        streamListener.processByte(readByte);
    }

    interface InputStreamListener {
        void processByte(int readByte);
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to get raw url from http request message on self hosted server?

From Dev

How to get only Message Body from a GET HTTP request?

From Dev

Change Response from HTTP Request sent by a program

From Dev

Reading multipart content from raw http request

From Dev

Reading multipart content from raw http request

From Dev

How to make an HTTP request from raw data?

From Dev

get a "raw" request\response from MITM Proxy

From Dev

Wcf get raw request from operation

From Dev

get a "raw" request\response from MITM Proxy

From Dev

How does JSON data get sent in an http request?

From Dev

HTTP Get Request from NodeJS

From Dev

HTTP Get Request from NodeJS

From Dev

Get request not sent

From Dev

Unable to get tokens from get_raw_request_token() in Rauth

From Dev

How can I get the http request headers actually sent by me in node.js http request

From Dev

How can I get the http request headers actually sent by me in node.js http request

From Dev

how to get message-id of email sent from smtplib

From Dev

get raw rfc822 message from phpmailer

From Dev

how to get parameter from request sent in JMS Jmeter

From Dev

If an HTTP request is sent from an iframe, where does the iframed site see the request from?

From Dev

How to get raw binary data from a POST request processed by Spring?

From Dev

How to get raw binary data from a POST request processed by Spring?

From Dev

Storing a HTTP POST request in a bookmark or something similar as you would with a GET request

From Dev

Get sent message in Outlook Addin

From Dev

Http.Request(new Request({})) sent twice

From Dev

Android JAVA HTTP GET Request - NetworkOnMainThreadException with NULL message

From Dev

How to get response message HTTP Request on Beanshell Jmeter

From Java

Get raw URL from Microsoft.AspNet.Http.HttpRequest

From Dev

Making HTTP GET request from Chrome Extension

Related Related

  1. 1

    How to get raw url from http request message on self hosted server?

  2. 2

    How to get only Message Body from a GET HTTP request?

  3. 3

    Change Response from HTTP Request sent by a program

  4. 4

    Reading multipart content from raw http request

  5. 5

    Reading multipart content from raw http request

  6. 6

    How to make an HTTP request from raw data?

  7. 7

    get a "raw" request\response from MITM Proxy

  8. 8

    Wcf get raw request from operation

  9. 9

    get a "raw" request\response from MITM Proxy

  10. 10

    How does JSON data get sent in an http request?

  11. 11

    HTTP Get Request from NodeJS

  12. 12

    HTTP Get Request from NodeJS

  13. 13

    Get request not sent

  14. 14

    Unable to get tokens from get_raw_request_token() in Rauth

  15. 15

    How can I get the http request headers actually sent by me in node.js http request

  16. 16

    How can I get the http request headers actually sent by me in node.js http request

  17. 17

    how to get message-id of email sent from smtplib

  18. 18

    get raw rfc822 message from phpmailer

  19. 19

    how to get parameter from request sent in JMS Jmeter

  20. 20

    If an HTTP request is sent from an iframe, where does the iframed site see the request from?

  21. 21

    How to get raw binary data from a POST request processed by Spring?

  22. 22

    How to get raw binary data from a POST request processed by Spring?

  23. 23

    Storing a HTTP POST request in a bookmark or something similar as you would with a GET request

  24. 24

    Get sent message in Outlook Addin

  25. 25

    Http.Request(new Request({})) sent twice

  26. 26

    Android JAVA HTTP GET Request - NetworkOnMainThreadException with NULL message

  27. 27

    How to get response message HTTP Request on Beanshell Jmeter

  28. 28

    Get raw URL from Microsoft.AspNet.Http.HttpRequest

  29. 29

    Making HTTP GET request from Chrome Extension

HotTag

Archive