org.apache.http.impl.DefaultBHttpClientConnection Java Examples

The following examples show how to use org.apache.http.impl.DefaultBHttpClientConnection. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ApacheHttp43SLR.java    From webarchive-commons with Apache License 2.0 4 votes vote down vote up
protected InputStream doSeekLoad(long offset, int maxLength, URL url)
          throws IOException {

try {
	SocketAddress endpoint = new InetSocketAddress(url.getHost(), getPort(url));
	
	socket = new Socket();
	socket.connect(endpoint, connectTimeout);
	
	activeConn = new DefaultBHttpClientConnection(BUFF_SIZE);
	activeConn.bind(socket);
	activeConn.setSocketTimeout(readTimeout);
	
	HttpRequest request = new BasicHttpRequest("GET", url.getFile(), HttpVersion.HTTP_1_1);
	
	String rangeHeader = makeRangeHeader(offset, maxLength);
	
	if (rangeHeader != null) {
		request.setHeader("Range", rangeHeader);
	}
	
	if (this.isNoKeepAlive()) {
		request.setHeader("Connection", "close");
	} else {
		request.setHeader("Connection", "keep-alive");
	}
	
	if (this.getCookie() != null) {
		request.setHeader("Cookie", this.getCookie());
	}
	
	request.setHeader("Accept", "*/*");
	request.setHeader("Host", url.getHost());
	
	activeConn.sendRequestHeader(request);
	activeConn.flush();
				
	response = activeConn.receiveResponseHeader();
	
	int code = response.getStatusLine().getStatusCode();
	
	connectedUrl = url.toString();
	
	if (code > 300 && code < 400) {
		Header header = response.getFirstHeader("Location");
		
		doClose();
		
		if (header != null) {
			URL redirectURL = new URL(header.getValue());
			return doSeekLoad(offset, maxLength, redirectURL);
		}
	}
	
	if (code != 200 && code != 206) {
		throw new BadHttpStatusException(code, connectedUrl + " " + rangeHeader);
	}
	
	activeConn.receiveResponseEntity(response);
	
	return response.getEntity().getContent();
	
} catch (HttpException e) {
	doClose();
	throw new IOException(e);
	
      } catch (IOException io) {
      	
	if (saveErrHeader != null) {
		errHeader = getHeaderValue(saveErrHeader);	
	}
	
	connectedUrl = url.toString();
	
	doClose();
	throw io;
      }
  }