【问题标题】:Get raw HTTP request message that would be sent from a HttpGet?获取将从 HttpGet 发送的原始 HTTP 请求消息?
【发布时间】:2014-08-11 11:27:54
【问题描述】:

如何在 Apache http 组件 v4.3.5 中获取完整的原始 HTTP 请求消息。 我设法只得到解析后的响应。

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);
    }
}

有一个类似的answer but for the old 3.1 version of apache commons-http v3.1。 v4.3.5 怎么办?

【问题讨论】:

    标签: java apache-commons-httpclient apache-httpcomponents


    【解决方案1】:

    让它像这样工作。不是最优雅的,因为流被复制到内存中,但适用于我的用例。

    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);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-16
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 2019-12-11
      • 2014-02-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多