org.apache.http.ProtocolException类的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(13.9k)|赞(0)|评价(0)|浏览(108)

本文整理了Java中org.apache.http.ProtocolException类的一些代码示例,展示了ProtocolException类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ProtocolException类的具体详情如下:
包路径:org.apache.http.ProtocolException
类名称:ProtocolException

ProtocolException介绍

[英]Signals that an HTTP protocol violation has occurred. For example a malformed status line or headers, a missing message body, etc.
[中]表示发生了HTTP协议冲突。例如,错误的状态行或标题、丢失的消息正文等。

代码示例

代码示例来源:origin: robovm/robovm

if (locationHeader == null) {
  throw new ProtocolException(
      "Received redirect response " + response.getStatusLine()
      + " but no location header");
  uri = new URI(location);            
} catch (URISyntaxException ex) {
  throw new ProtocolException("Invalid redirect URI: " + location, ex);
if (!uri.isAbsolute()) {
  if (params.isParameterTrue(ClientPNames.REJECT_RELATIVE_REDIRECT)) {
    throw new ProtocolException("Relative redirect location '" 
        + uri + "' not allowed");
    URI requestURI = new URI(request.getRequestLine().getUri());
    URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, true);
    uri = URIUtils.resolve(absoluteRequestURI, uri); 
  } catch (URISyntaxException ex) {
    throw new ProtocolException(ex.getMessage(), ex);
      HttpHost target = new HttpHost(
      redirectURI = URIUtils.rewriteURI(uri, target, true);
    } catch (URISyntaxException ex) {
      throw new ProtocolException(ex.getMessage(), ex);

代码示例来源:origin: ibinti/bugvm

final HttpResponse response,
  final HttpContext context) throws ProtocolException {
Args.notNull(request, "HTTP request");
Args.notNull(response, "HTTP response");
Args.notNull(context, "HTTP context");
if (locationHeader == null) {
  throw new ProtocolException(
      "Received redirect response " + response.getStatusLine()
      + " but no location header");
  if (!uri.isAbsolute()) {
    if (!config.isRelativeRedirectsAllowed()) {
      throw new ProtocolException("Relative redirect location '"
          + uri + "' not allowed");
    final URI requestURI = new URI(request.getRequestLine().getUri());
    final URI absoluteRequestURI = URIUtils.rewriteURI(requestURI, target, false);
    uri = URIUtils.resolve(absoluteRequestURI, uri);
  throw new ProtocolException(ex.getMessage(), ex);
if (redirectLocations == null) {
  redirectLocations = new RedirectLocations();
  context.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations);

代码示例来源:origin: robovm/robovm

CookieStore cookieStore = (CookieStore) context.getAttribute(
    ClientContext.COOKIE_STORE);
if (cookieStore == null) {
CookieSpecRegistry registry= (CookieSpecRegistry) context.getAttribute(
    ClientContext.COOKIESPEC_REGISTRY);
if (registry == null) {
HttpHost targetHost = (HttpHost) context.getAttribute(
    ExecutionContext.HTTP_TARGET_HOST);
if (targetHost == null) {
String policy = HttpClientParams.getCookiePolicy(request.getParams());
if (this.log.isDebugEnabled()) {
  this.log.debug("CookieSpec selected: " + policy);
} else {
  try {
    requestURI = new URI(request.getRequestLine().getUri());
  } catch (URISyntaxException ex) {
    throw new ProtocolException("Invalid request URI: " + 
        request.getRequestLine().getUri(), ex);
String hostName = targetHost.getHostName();
int port = targetHost.getPort();
if (port < 0) {
  port = conn.getRemotePort();

代码示例来源:origin: ibinti/bugvm

public RequestWrapper(final HttpRequest request) throws ProtocolException {
  super();
  Args.notNull(request, "HTTP request");
  this.original = request;
  setParams(request.getParams());
  setHeaders(request.getAllHeaders());
  // Make a copy of the original URI
  if (request instanceof HttpUriRequest) {
    this.uri = ((HttpUriRequest) request).getURI();
    this.method = ((HttpUriRequest) request).getMethod();
    this.version = null;
  } else {
    final RequestLine requestLine = request.getRequestLine();
    try {
      this.uri = new URI(requestLine.getUri());
    } catch (final URISyntaxException ex) {
      throw new ProtocolException("Invalid request URI: "
          + requestLine.getUri(), ex);
    }
    this.method = requestLine.getMethod();
    this.version = request.getProtocolVersion();
  }
  this.execCount = 0;
}

代码示例来源:origin: robovm/robovm

public RequestWrapper(final HttpRequest request) throws ProtocolException {
  super();
  if (request == null) {
    throw new IllegalArgumentException("HTTP request may not be null");
  }
  this.original = request;
  setParams(request.getParams());
  // Make a copy of the original URI 
  if (request instanceof HttpUriRequest) {
    this.uri = ((HttpUriRequest) request).getURI();
    this.method = ((HttpUriRequest) request).getMethod();
    this.version = null;
  } else {
    RequestLine requestLine = request.getRequestLine();
    try {
      this.uri = new URI(requestLine.getUri());
    } catch (URISyntaxException ex) {
      throw new ProtocolException("Invalid request URI: " 
          + requestLine.getUri(), ex);
    }
    this.method = requestLine.getMethod();
    this.version = request.getProtocolVersion();
  }
  this.execCount = 0;
}

代码示例来源:origin: com.hubspot/HorizonApache

@Override
protected URI createLocationURI(String location) throws ProtocolException {
 try {
  return new URI(location).normalize();
 } catch (URISyntaxException e) {
  try {
   return parseLenient(location).normalize();
  } catch (URISyntaxException f) {
   throw new ProtocolException("Invalid redirect URI: " + location, e);
  }
 }
}

代码示例来源:origin: org.apache.httpcomponents/httpclient-android

final HttpResponse response,
  final HttpContext context) throws ProtocolException {
Args.notNull(request, "HTTP request");
Args.notNull(response, "HTTP response");
Args.notNull(context, "HTTP context");
if (locationHeader == null) {
  throw new ProtocolException(
      "Received redirect response " + response.getStatusLine()
      + " but no location header");
  if (!uri.isAbsolute()) {
    if (!config.isRelativeRedirectsAllowed()) {
      throw new ProtocolException("Relative redirect location '"
          + uri + "' not allowed");
    final URI requestURI = new URI(request.getRequestLine().getUri());
    final URI absoluteRequestURI = URIUtilsHC4.rewriteURI(requestURI, target, false);
    uri = URIUtilsHC4.resolve(absoluteRequestURI, uri);
  throw new ProtocolException(ex.getMessage(), ex);
if (redirectLocations == null) {
  redirectLocations = new RedirectLocationsHC4();
  context.setAttribute(HttpClientContext.REDIRECT_LOCATIONS, redirectLocations);

代码示例来源:origin: robolectric/robolectric

protected void rewriteRequestURI(
  final RequestWrapper request,
  final HttpRoute route) throws ProtocolException {
 try {
  URI uri = request.getURI();
  if (route.getProxyHost() != null && !route.isTunnelled()) {
   // Make sure the request URI is absolute
   if (!uri.isAbsolute()) {
    HttpHost target = route.getTargetHost();
    uri = URIUtils.rewriteURI(uri, target);
    request.setURI(uri);
   }
  } else {
   // Make sure the request URI is relative
   if (uri.isAbsolute()) {
    uri = URIUtils.rewriteURI(uri, null);
    request.setURI(uri);
   }
  }
 } catch (URISyntaxException ex) {
  throw new ProtocolException("Invalid URI: " +
    request.getRequestLine().getUri(), ex);
 }
}

代码示例来源:origin: ibinti/bugvm

@Override
public void process(final HttpRequest request, final HttpContext context)
    throws HttpException, IOException {
  Args.notNull(request, "HTTP request");
  final ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
  final String method = request.getRequestLine().getMethod();
  if (method.equalsIgnoreCase("CONNECT") && ver.lessEquals(HttpVersion.HTTP_1_0)) {
    return;
  if (!request.containsHeader(HTTP.TARGET_HOST)) {
    HttpHost targethost = corecontext.getTargetHost();
    if (targethost == null) {
        final int port = ((HttpInetConnection) conn).getRemotePort();
        if (address != null) {
          targethost = new HttpHost(address.getHostName(), port);
        if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
          return;
        } else {
          throw new ProtocolException("Target host missing");
    request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());

代码示例来源:origin: robovm/robovm

throw new IllegalArgumentException("HTTP context may not be null");
if (!request.containsHeader(HTTP.TARGET_HOST)) {
  HttpHost targethost = (HttpHost) context
    .getAttribute(ExecutionContext.HTTP_TARGET_HOST);
  if (targethost == null) {
    HttpConnection conn = (HttpConnection) context
      .getAttribute(ExecutionContext.HTTP_CONNECTION);
    if (conn instanceof HttpInetConnection) {
      int port = ((HttpInetConnection) conn).getRemotePort();
      if (address != null) {
        targethost = new HttpHost(address.getHostName(), port);
      ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
      if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
        return;
      } else {
        throw new ProtocolException("Target host missing");
  request.addHeader(HTTP.TARGET_HOST, targethost.toHostString());

代码示例来源:origin: org.apache.httpcomponents/httpclient-android

public void process(final HttpRequest request, final HttpContext context)
    throws HttpException, IOException {
  Args.notNull(request, "HTTP request");
  if (request instanceof HttpEntityEnclosingRequest) {
    if (this.overwrite) {
      request.removeHeaders(HTTP.TRANSFER_ENCODING);
      request.removeHeaders(HTTP.CONTENT_LEN);
    } else {
      if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
        throw new ProtocolException("Transfer-encoding header already present");
      if (request.containsHeader(HTTP.CONTENT_LEN)) {
        throw new ProtocolException("Content-Length header already present");
    final ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
    final HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
    if (entity == null) {
      if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
        throw new ProtocolException(
            "Chunked transfer encoding not allowed for " + ver);

代码示例来源:origin: org.apache.httpcomponents/httpclient-android

static void rewriteRequestURI(
    final HttpRequestWrapper request,
    final HttpRoute route) throws ProtocolException {
  try {
    URI uri = request.getURI();
    if (uri != null) {
      // Make sure the request URI is relative
      if (uri.isAbsolute()) {
        uri = URIUtilsHC4.rewriteURI(uri, null, true);
      } else {
        uri = URIUtilsHC4.rewriteURI(uri);
      }
      request.setURI(uri);
    }
  } catch (final URISyntaxException ex) {
    throw new ProtocolException("Invalid URI: " + request.getRequestLine().getUri(), ex);
  }
}

代码示例来源:origin: ibinti/bugvm

final HttpClientConnection conn,
  final HttpContext context) throws IOException, HttpException {
Args.notNull(request, "HTTP request");
Args.notNull(conn, "Client connection");
Args.notNull(context, "HTTP context");
context.setAttribute(HttpCoreContext.HTTP_CONNECTION, conn);
context.setAttribute(HttpCoreContext.HTTP_REQ_SENT, Boolean.FALSE);
    request.getRequestLine().getProtocolVersion();
  if (((HttpEntityEnclosingRequest) request).expectContinue() &&
    !ver.lessEquals(HttpVersion.HTTP_1_0)) {
      if (status < 200) {
        if (status != HttpStatus.SC_CONTINUE) {
          throw new ProtocolException(
              "Unexpected response: " + response.getStatusLine());
context.setAttribute(HttpCoreContext.HTTP_REQ_SENT, Boolean.TRUE);
return response;

代码示例来源:origin: robovm/robovm

if (request.containsHeader(HTTP.TRANSFER_ENCODING)) {
  throw new ProtocolException("Transfer-encoding header already present");
if (request.containsHeader(HTTP.CONTENT_LEN)) {
  throw new ProtocolException("Content-Length header already present");
ProtocolVersion ver = request.getRequestLine().getProtocolVersion();
HttpEntity entity = ((HttpEntityEnclosingRequest)request).getEntity();
if (entity == null) {
  if (ver.lessEquals(HttpVersion.HTTP_1_0)) {
    throw new ProtocolException(
        "Chunked transfer encoding not allowed for " + ver);

代码示例来源:origin: ibinti/bugvm

final HttpClientContext context,
  final HttpExecutionAware execAware) throws IOException, HttpException {
Args.notNull(route, "HTTP route");
Args.notNull(request, "HTTP request");
Args.notNull(context, "HTTP context");
      if (!redirect.headerIterator().hasNext()) {
        final HttpRequest original = request.getOriginal();
        redirect.setHeaders(original.getAllHeaders());
      final HttpHost newTarget = URIUtils.extractHost(uri);
      if (newTarget == null) {
        throw new ProtocolException("Redirect URI does not specify a valid host name: " +
            uri);
      if (!currentRoute.getTargetHost().equals(newTarget)) {
        final AuthState targetAuthState = context.getTargetAuthState();
        if (targetAuthState != null) {

代码示例来源:origin: org.apache.httpcomponents/httpclient-android

public long determineLength(final HttpMessage message) throws HttpException {
  Args.notNull(message, "HTTP message");
    final String s = transferEncodingHeader.getValue();
    if (HTTP.CHUNK_CODING.equalsIgnoreCase(s)) {
      if (message.getProtocolVersion().lessEquals(HttpVersion.HTTP_1_0)) {
        throw new ProtocolException(
            "Chunked transfer encoding not allowed for " +
            message.getProtocolVersion());
      return IDENTITY;
    } else {
      throw new ProtocolException(
          "Unsupported transfer encoding: " + s);
      final long len = Long.parseLong(s);
      if (len < 0) {
        throw new ProtocolException("Negative content length: " + s);
      throw new ProtocolException("Invalid content length: " + s);

代码示例来源:origin: robovm/robovm

String s = transferEncodingHeader.getValue();
if (HTTP.CHUNK_CODING.equalsIgnoreCase(s)) {
  if (message.getProtocolVersion().lessEquals(HttpVersion.HTTP_1_0)) {
    throw new ProtocolException(
        "Chunked transfer encoding not allowed for " + 
        message.getProtocolVersion());
  return IDENTITY;
} else {
  throw new ProtocolException(
      "Unsupported transfer encoding: " + s);
  return len;
} catch (NumberFormatException e) {
  throw new ProtocolException("Invalid content length: " + s);

代码示例来源:origin: robovm/robovm

encodings = transferEncodingHeader.getElements();
} catch (ParseException px) {
  throw new ProtocolException
    ("Invalid Transfer-Encoding header value: " +
     transferEncodingHeader, px);
      && !encoding.equalsIgnoreCase(HTTP.CHUNK_CODING)
      && !encoding.equalsIgnoreCase(HTTP.IDENTITY_CODING)) {
      throw new ProtocolException("Unsupported transfer encoding: " + encoding);
} else {
  if (strict) {
    throw new ProtocolException("Chunk-encoding must be the last one applied");
Header[] headers = message.getHeaders(HTTP.CONTENT_LEN);
if (strict && headers.length > 1) {
  throw new ProtocolException("Multiple content length headers");
  } catch (NumberFormatException e) {
    if (strict) {
      throw new ProtocolException("Invalid content length: " + header.getValue());

代码示例来源:origin: com.hynnet/httpclient

void rewriteRequestURI(
    final HttpRequestWrapper request,
    final HttpRoute route) throws ProtocolException {
  final URI uri = request.getURI();
  if (uri != null) {
    try {
      request.setURI(URIUtils.rewriteURIForRoute(uri, route));
    } catch (final URISyntaxException ex) {
      throw new ProtocolException("Invalid URI: " + uri, ex);
    }
  }
}

代码示例来源:origin: org.wildfly/wildfly-testsuite-shared

@Override
  public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) {
    boolean isRedirect = false;
    try {
      isRedirect = super.isRedirected(request, response, context);
    } catch (ProtocolException e) {
      e.printStackTrace();
    }
    if (!isRedirect) {
      final int responseCode = response.getStatusLine().getStatusCode();
      isRedirect = (responseCode == 301 || responseCode == 302);
    }
    return isRedirect;
  }
};

相关文章