Java tutorial
package org.apache.http.impl; import java.io.IOException; import java.io.OutputStream; import java.net.Socket; import java.net.SocketTimeoutException; import java.nio.charset.CharsetDecoder; import java.nio.charset.CharsetEncoder; import org.apache.http.HttpClientConnection; import org.apache.http.HttpEntity; import org.apache.http.HttpEntityEnclosingRequest; import org.apache.http.HttpException; import org.apache.http.HttpRequest; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.annotation.NotThreadSafe; import org.apache.http.config.MessageConstraints; import org.apache.http.entity.ContentLengthStrategy; import org.apache.http.impl.io.DefaultHttpRequestWriterFactory; import org.apache.http.impl.io.DefaultHttpResponseParserFactory; import org.apache.http.io.HttpMessageParser; import org.apache.http.io.HttpMessageParserFactory; import org.apache.http.io.HttpMessageWriter; import org.apache.http.io.HttpMessageWriterFactory; import org.apache.http.util.Args; @NotThreadSafe public class DefaultBHttpClientConnection extends BHttpConnectionBase implements HttpClientConnection { private final HttpMessageWriter<HttpRequest> requestWriter; private final HttpMessageParser<HttpResponse> responseParser; public DefaultBHttpClientConnection(int i, int i2, CharsetDecoder charsetDecoder, CharsetEncoder charsetEncoder, MessageConstraints messageConstraints, ContentLengthStrategy contentLengthStrategy, ContentLengthStrategy contentLengthStrategy2, HttpMessageWriterFactory<HttpRequest> httpMessageWriterFactory, HttpMessageParserFactory<HttpResponse> httpMessageParserFactory) { super(i, i2, charsetDecoder, charsetEncoder, messageConstraints, contentLengthStrategy, contentLengthStrategy2); if (httpMessageWriterFactory == null) { httpMessageWriterFactory = DefaultHttpRequestWriterFactory.INSTANCE; } this.requestWriter = httpMessageWriterFactory.create(getSessionOutputBuffer()); if (httpMessageParserFactory == null) { httpMessageParserFactory = DefaultHttpResponseParserFactory.INSTANCE; } this.responseParser = httpMessageParserFactory.create(getSessionInputBuffer(), messageConstraints); } public DefaultBHttpClientConnection(int i, CharsetDecoder charsetDecoder, CharsetEncoder charsetEncoder, MessageConstraints messageConstraints) { this(i, i, charsetDecoder, charsetEncoder, messageConstraints, null, null, null, null); } public DefaultBHttpClientConnection(int i) { this(i, i, null, null, null, null, null, null, null); } protected void onResponseReceived(HttpResponse httpResponse) { } protected void onRequestSubmitted(HttpRequest httpRequest) { } public void bind(Socket socket) throws IOException { super.bind(socket); } public boolean isResponseAvailable(int i) throws IOException { ensureOpen(); try { return awaitInput(i); } catch (SocketTimeoutException e) { return false; } } public void sendRequestHeader(HttpRequest httpRequest) throws HttpException, IOException { Args.notNull(httpRequest, "HTTP request"); ensureOpen(); this.requestWriter.write(httpRequest); onRequestSubmitted(httpRequest); incrementRequestCount(); } public void sendRequestEntity(HttpEntityEnclosingRequest httpEntityEnclosingRequest) throws HttpException, IOException { Args.notNull(httpEntityEnclosingRequest, "HTTP request"); ensureOpen(); HttpEntity entity = httpEntityEnclosingRequest.getEntity(); if (entity != null) { OutputStream prepareOutput = prepareOutput(httpEntityEnclosingRequest); entity.writeTo(prepareOutput); prepareOutput.close(); } } public HttpResponse receiveResponseHeader() throws HttpException, IOException { ensureOpen(); HttpResponse httpResponse = (HttpResponse) this.responseParser.parse(); onResponseReceived(httpResponse); if (httpResponse.getStatusLine().getStatusCode() >= HttpStatus.SC_OK) { incrementResponseCount(); } return httpResponse; } public void receiveResponseEntity(HttpResponse httpResponse) throws HttpException, IOException { Args.notNull(httpResponse, "HTTP response"); ensureOpen(); httpResponse.setEntity(prepareInput(httpResponse)); } public void flush() throws IOException { ensureOpen(); doFlush(); } }