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();
}
On 6 March 2012 09:21, Ben Short <[email protected]> wrote:
> Hi,
>
> I need to perform http requests over a supplied InputStream and
> OutputStream. I've had a look through the code and it seems like it
> should be possible if I extend AbstractHttpClientConnection.
>
> Does this sounds like a reasonable approach?
>
> Ben
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]