On Thu, 2012-03-15 at 11:44 +0000, Ben Short wrote: > Could anyone point me on the right direction to use these code changes with > the HttpClient rather than having to use the HttpCore? > > Ben >
You will need a custom connection manager with a custom connection operator. Oleg > On 6 March 2012 21:00, Ben Short <[email protected]> wrote: > > > Thanks Oleg. > > > > On 6 March 2012 15:28, Oleg Kalnichevski <[email protected]> wrote: > > > On Tue, Mar 06, 2012 at 01:20:19PM +0000, Ben Short wrote: > > >> Hi, > > >> > > >> I have something working as shown below. The input and output streams > > >> are managed outside of the doHttpRequest. Does this seam like a > > >> reasonable solution? > > >> > > >> public class InputStreamSessionInputBuffer extends > > AbstractSessionInputBuffer { > > >> > > >> public InputStreamSessionInputBuffer(final InputStream is, final > > >> HttpParams params) { > > >> super(); > > >> init(is, 1024, params); > > >> } > > >> > > >> public boolean isDataAvailable(int timeout) throws IOException { > > >> boolean result = hasBufferedData(); > > >> if (!result) { > > >> fillBuffer(); > > >> result = hasBufferedData(); > > >> } > > >> return result; > > >> } > > >> } > > >> > > >> > > >> public class OutputStreamSessionInputBuffer extends > > >> AbstractSessionOutputBuffer { > > >> > > >> public OutputStreamSessionInputBuffer(final OutputStream os, final > > >> HttpParams params) { > > >> super(); > > >> init(os, 1024, params); > > >> } > > >> } > > >> > > >> public class StreamHttpClientConnection extends > > AbstractHttpClientConnection { > > >> > > >> private boolean open = false; > > >> private InputStream is; > > >> private OutputStream os; > > >> private SessionInputBuffer inputBuffer; > > >> private SessionOutputBuffer outputBuffer; > > >> > > >> public StreamHttpClientConnection() { > > >> } > > >> > > >> public void init(InputStream is, OutputStream os, HttpParams > > >> params) throws IOException { > > >> > > >> this.is = is; > > >> this.os = os; > > >> > > >> this.inputBuffer = new InputStreamSessionInputBuffer(is, > > params); > > >> this.outputBuffer = new OutputStreamSessionInputBuffer(os, > > params); > > >> > > >> init(this.inputBuffer, this.outputBuffer, params); > > >> > > >> this.open = true; > > >> } > > >> > > >> @Override > > >> protected void assertOpen() throws IllegalStateException { > > >> if (!this.open) { > > >> throw new IllegalStateException("Connection is not open"); > > >> } > > >> } > > >> > > >> public void close() throws IOException { > > >> if (!this.open) { > > >> return; > > >> } > > >> this.open = false; > > >> > > >> this.os.flush(); > > >> } > > >> > > >> public boolean isOpen() { > > >> return this.open; > > >> } > > >> > > >> public void setSocketTimeout(int i) { > > >> } > > >> > > >> public int getSocketTimeout() { > > >> return -1; > > >> } > > >> > > >> public void shutdown() throws IOException { > > >> this.close(); > > >> } > > >> } > > >> > > >> > > >> public static void doHttpRequest(InputStream is, OutputStream os) > > >> throws IOException, HttpException { > > >> > > >> HttpParams params = new SyncBasicHttpParams(); > > >> HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); > > >> HttpProtocolParams.setContentCharset(params, "UTF-8"); > > >> HttpProtocolParams.setUserAgent(params, "HttpComponents/1.1"); > > >> HttpProtocolParams.setUseExpectContinue(params, true); > > >> > > >> HttpProcessor httpproc = new ImmutableHttpProcessor(new > > >> HttpRequestInterceptor[] { > > >> // Required protocol interceptors > > >> new RequestContent(), > > >> new RequestTargetHost(), > > >> // Recommended protocol interceptors > > >> new RequestConnControl(), > > >> new RequestUserAgent(), > > >> new RequestExpectContinue()}); > > >> > > >> HttpRequestExecutor httpexecutor = new HttpRequestExecutor(); > > >> > > >> HttpContext context = new BasicHttpContext(null); > > >> HttpHost host = new HttpHost("10.10.20.60", 4050); > > >> > > >> StreamHttpClientConnection conn = new > > StreamHttpClientConnection(); > > >> > > >> //DefaultHttpClientConnection conn = new > > DefaultHttpClientConnection(); > > >> ConnectionReuseStrategy connStrategy = new > > >> DefaultConnectionReuseStrategy(); > > >> > > >> context.setAttribute(ExecutionContext.HTTP_CONNECTION, conn); > > >> context.setAttribute(ExecutionContext.HTTP_TARGET_HOST, host); > > >> > > >> conn.init(is, os, params); > > >> > > >> //conn.bind(new Socket(host.getHostName(), host.getPort()), > > params); > > >> > > >> HttpRequest request = new BasicHttpRequest("GET", > > >> "/messages?validity=active_only"); > > >> > > >> httpexecutor.preProcess(request, httpproc, context); > > >> HttpResponse response = httpexecutor.execute(request, conn, > > context); > > >> response.setParams(params); > > >> httpexecutor.postProcess(response, httpproc, context); > > >> > > >> System.out.println("<< Response: " + response.getStatusLine()); > > >> System.out.println(EntityUtils.toString(response.getEntity())); > > >> > > >> conn.shutdown(); > > >> } > > >> > > > > > > Looks all right to me. > > > > > > Cheers > > > > > > Oleg > > > > > > --------------------------------------------------------------------- > > > To unsubscribe, e-mail: [email protected] > > > For additional commands, e-mail: [email protected] > > > > > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
