HttpClient { public StreamingResponse fetchResponse(StreamingRequest request) throws IOException, MessageFormatException { StreamingResponse response = connect(request.getTarget(), request .isSsl()); if (response != null) return response; response = new StreamingResponse.Impl(); sendRequestHeader(request.getHeader()); request.setTime(getRequestTime()); if (MessageUtils.isExpectContinue(request)) { socket.setSoTimeout(2000); try { response.setHeader(getResponseHeader()); response.setHeaderTime(getResponseHeaderEndTime()); } catch (SocketTimeoutException ste) { } finally { socket.setSoTimeout(getSoTimeout()); } if (response.getHeader() != null && !"100".equals(response.getStatus())) { InputStream content = getResponseContent(); if (content != null) content = new TimingInputStream(content, response); response.setContent(content); return response; } if (request.getContent() != null) { sendRequestContent(request.getContent()); request.setTime(getRequestTime()); } } else { if (request.getContent() != null) sendRequestContent(request.getContent()); request.setTime(System.currentTimeMillis()); } if (response.getHeader() != null) { byte[] cont = response.getHeader(); byte[] header = getResponseHeader(); response.setHeaderTime(getResponseHeaderEndTime()); byte[] newHeader = new byte[cont.length + header.length]; System.arraycopy(cont, 0, newHeader, 0, cont.length); System.arraycopy(header, 0, newHeader, cont.length, header.length); response.setHeader(newHeader); } else { response.setHeader(getResponseHeader()); response.setHeaderTime(getResponseHeaderEndTime()); } InputStream content = getResponseContent(); if (content != null) content = new TimingInputStream(content, response); response.setContent(content); return response; } HttpClient(); void setProxySelector(ProxySelector proxySelector); ProxySelector getProxySelector(); void setSslEnabledProtocols(String[] protocols); String[] getSslEnabledProtocols(); void setSoTimeout(int timeout); int getSoTimeout(); void setSslContextSelector(SSLContextSelector contextSelector); void setAddressResolver(AddressResolver resolver); State getState(); StreamingResponse connect(String host, int port, boolean ssl); StreamingResponse connect(InetSocketAddress target, boolean ssl); void sendRequestHeader(byte[] header); void sendRequestContent(byte[] content); void sendRequestContent(InputStream content); byte[] getResponseHeader(); InputStream getResponseContent(); void disconnect(); long getRequestTime(); long getResponseHeaderStartTime(); long getResponseHeaderEndTime(); StreamingResponse fetchResponse(StreamingRequest request); static final ProxySelector NO_PROXY; }
@Test public void testFetchResponse() throws Exception { HttpClient client = new HttpClient(); client.connect("localhost", 9999, false); String request = "GET /blah/blah?abc=def HTTP/1.0\r\nHost: localhost\r\n\r\n"; client.sendRequestHeader(request.getBytes()); byte[] header = client.getResponseHeader(); logger.fine("Header length: " + header.length); int got, read = 0; byte[] buff = new byte[1024]; InputStream is = client.getResponseContent(); while ((got = is.read(buff)) > 0) read = read + got; assertEquals(request.length(), read); }