org.apache.http.HttpResponse.setStatusCode()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(11.1k)|赞(0)|评价(0)|浏览(122)

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

HttpResponse.setStatusCode介绍

[英]Updates the status line of this response with a new status code. The status line can only be updated if it is available. It must have been set either explicitly or in a constructor.
The reason phrase will be updated according to the new status code, based on the current #getLocale. It can be set explicitly using #setReasonPhrase.
[中]使用新的状态代码更新此响应的状态行。状态行只有在可用时才能更新。它必须已显式或在构造函数中设置。
原因短语将根据新的状态代码根据当前的#getLocale进行更新。可以使用#setReasonPhrase显式设置。

代码示例

代码示例来源:origin: rest-assured/rest-assured

public void setStatusCode( int arg0 ) throws IllegalStateException {
  responseBase.setStatusCode( arg0 );
}

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

protected void handleException(final HttpException ex, final HttpResponse response) {
  if (ex instanceof MethodNotSupportedException) {
    response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
  } else if (ex instanceof UnsupportedHttpVersionException) {
    response.setStatusCode(HttpStatus.SC_HTTP_VERSION_NOT_SUPPORTED);
  } else if (ex instanceof ProtocolException) {
    response.setStatusCode(HttpStatus.SC_BAD_REQUEST);
  } else {
    response.setStatusCode(HttpStatus.SC_INTERNAL_SERVER_ERROR);
  }
  byte[] msg = EncodingUtils.getAsciiBytes(ex.getMessage());
  ByteArrayEntity entity = new ByteArrayEntity(msg);
  entity.setContentType("text/plain; charset=US-ASCII");
  response.setEntity(entity);
}

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

public static void fileToHttpResponse(String fullPathFile, HttpResponse response, String contentType, boolean useFileChannels) {
  int status;
  final File file = new File(fullPathFile);
  if (!file.exists())
    status = HttpStatus.SC_NOT_FOUND;
  else if (!file.canRead() || file.isDirectory())
    status = HttpStatus.SC_FORBIDDEN;
  else {
    status = HttpStatus.SC_OK;
    NFileEntity entity = new NFileEntity(file, contentType, useFileChannels);
    response.setEntity(entity);
  }
  response.setStatusCode(status);
}

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

public static void fileToHttpResponse(String fullPathFile, HttpResponse response, String contentType, boolean useFileChannels) {
  int status;
  final File file = new File(fullPathFile);
  if (!file.exists())
    status = HttpStatus.SC_NOT_FOUND;
  else if (!file.canRead() || file.isDirectory())
    status = HttpStatus.SC_FORBIDDEN;
  else {
    status = HttpStatus.SC_OK;
    NFileEntity entity = new NFileEntity(file, contentType, useFileChannels);
    response.setEntity(entity);
  }
  response.setStatusCode(status);
}

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

public static void toHttpResponse(InputStream stream, HttpResponse response, String contentType, long streamLength)
    throws IOException {
  InputStreamEntity body = new InputStreamEntity(stream, streamLength);
  body.setContentType(contentType);
  response.setEntity(body);
  response.setStatusCode(HttpStatus.SC_OK);
}

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

public static void toHttpResponse(InputStream stream, HttpResponse response, String contentType, long streamLength)
    throws IOException {
  InputStreamEntity body = new InputStreamEntity(stream, streamLength);
  body.setContentType(contentType);
  response.setEntity(body);
  response.setStatusCode(HttpStatus.SC_OK);
}

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

public static void errorInternalServerError(HttpResponse response, String message, Throwable exception) {
  int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
  response.setStatusCode(status);
  StringWriter sw = new StringWriter();
  exception.printStackTrace(new PrintWriter(sw, true));
  String logMessage = (message != null ? message + "\n" : "") + sw.toString();
  logger.debug("Returning HTTP status " + status + ": " + logMessage);
  try {
    NStringEntity entity = new NStringEntity("<html><body><h1>Internal server error</h1><p>"
        + (message != null ? message : "") + "<pre>" + sw.toString() + "</pre></body></html>", "UTF-8");
    entity.setContentType("text/html; charset=UTF-8");
    response.setEntity(entity);
  } catch (UnsupportedEncodingException e) {
  }
}

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

public static void errorInternalServerError(HttpResponse response, String message, Throwable exception) {
  int status = HttpStatus.SC_INTERNAL_SERVER_ERROR;
  response.setStatusCode(status);
  StringWriter sw = new StringWriter();
  exception.printStackTrace(new PrintWriter(sw, true));
  String logMessage = (message != null ? message + "\n" : "") + sw.toString();
  logger.debug("Returning HTTP status " + status + ": " + logMessage);
  try {
    NStringEntity entity = new NStringEntity("<html><body><h1>Internal server error</h1><p>"
        + (message != null ? message : "") + "<pre>" + sw.toString() + "</pre></body></html>", "UTF-8");
    entity.setContentType("text/html; charset=UTF-8");
    response.setEntity(entity);
  } catch (UnsupportedEncodingException e) {
  }
}

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

public static void errorFileNotFound(HttpResponse response, String uri) {
  int status = HttpStatus.SC_NOT_FOUND;
  response.setStatusCode(status);
  String message = "File " + uri + " not found";
  logger.debug("Returning HTTP status " + status + ": " + message);
  try {
    NStringEntity entity = new NStringEntity("<html><body><h1>File not found</h1><p>" + message + "</p></body></html>",
        "UTF-8");
    entity.setContentType("text/html; charset=UTF-8");
    response.setEntity(entity);
  } catch (UnsupportedEncodingException e) {
  }
}

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

public static void errorMissingQueryParameter(HttpResponse response, String param) {
  int status = HttpStatus.SC_BAD_REQUEST;
  response.setStatusCode(status);
  String message = "Request must contain the parameter " + param;
  logger.debug("Returning HTTP status " + status + ": " + message);
  try {
    NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>",
        "UTF-8");
    entity.setContentType("text/html; charset=UTF-8");
    response.setEntity(entity);
  } catch (UnsupportedEncodingException e) {
  }
}

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

public static void errorMissingQueryParameter(HttpResponse response, String param) {
  int status = HttpStatus.SC_BAD_REQUEST;
  response.setStatusCode(status);
  String message = "Request must contain the parameter " + param;
  logger.debug("Returning HTTP status " + status + ": " + message);
  try {
    NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>",
        "UTF-8");
    entity.setContentType("text/html; charset=UTF-8");
    response.setEntity(entity);
  } catch (UnsupportedEncodingException e) {
  }
}

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

public static void errorFileNotFound(HttpResponse response, String uri) {
  int status = HttpStatus.SC_NOT_FOUND;
  response.setStatusCode(status);
  String message = "File " + uri + " not found";
  logger.debug("Returning HTTP status " + status + ": " + message);
  try {
    NStringEntity entity = new NStringEntity("<html><body><h1>File not found</h1><p>" + message + "</p></body></html>",
        "UTF-8");
    entity.setContentType("text/html; charset=UTF-8");
    response.setEntity(entity);
  } catch (UnsupportedEncodingException e) {
  }
}

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

protected void doService(
    final HttpRequest request, 
    final HttpResponse response,
    final HttpContext context) throws HttpException, IOException {
  HttpRequestHandler handler = null;
  if (this.handlerResolver != null) {
    String requestURI = request.getRequestLine().getUri();
    handler = this.handlerResolver.lookup(requestURI);
  }
  if (handler != null) {
    handler.handle(request, response, context);
  } else {
    response.setStatusCode(HttpStatus.SC_NOT_IMPLEMENTED);
  }
}

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

public static void errorWrongQueryParameterValue(HttpResponse response, String paramName, String illegalValue,
    String explanation) {
  int status = HttpStatus.SC_BAD_REQUEST;
  response.setStatusCode(status);
  String message = "The value '" + illegalValue + "' of parameter '" + paramName + "' is not valid"
      + (explanation != null ? ": " + explanation : "");
  logger.debug("Returning HTTP status " + status + ": " + message);
  try {
    NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>",
        "UTF-8");
    entity.setContentType("text/html; charset=UTF-8");
    response.setEntity(entity);
  } catch (UnsupportedEncodingException e) {
  }
}

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

public static void errorWrongQueryParameterValue(HttpResponse response, String paramName, String illegalValue,
    String explanation) {
  int status = HttpStatus.SC_BAD_REQUEST;
  response.setStatusCode(status);
  String message = "The value '" + illegalValue + "' of parameter '" + paramName + "' is not valid"
      + (explanation != null ? ": " + explanation : "");
  logger.debug("Returning HTTP status " + status + ": " + message);
  try {
    NStringEntity entity = new NStringEntity("<html><body><h1>Bad request</h1><p>" + message + ".</h1></body></html>",
        "UTF-8");
    entity.setContentType("text/html; charset=UTF-8");
    response.setEntity(entity);
  } catch (UnsupportedEncodingException e) {
  }
}

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

public static void toHttpResponse(byte[] byteArray, HttpResponse response, String contentType) throws IOException {
  NByteArrayEntity body = new NByteArrayEntity(byteArray);
  body.setContentType(contentType);
  response.setEntity(body);
  response.setStatusCode(HttpStatus.SC_OK);
}

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

public static void toHttpResponse(byte[] byteArray, HttpResponse response, String contentType) throws IOException {
  NByteArrayEntity body = new NByteArrayEntity(byteArray);
  body.setContentType(contentType);
  response.setEntity(body);
  response.setStatusCode(HttpStatus.SC_OK);
}

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

@Override
protected void handleClientRequest(String absPath, Map<String, String> queryItems, HttpResponse response,
    Address serverAddressAtClient) throws IOException {
  // Individual info request
  String infoResponse = handleInfoRequest(absPath, queryItems, response);
  if (infoResponse == null) { // error condition, handleInfoRequest has set an error message
    return;
  }
  response.setStatusCode(HttpStatus.SC_OK);
  try {
    NStringEntity entity = new NStringEntity(infoResponse, "UTF-8");
    entity.setContentType("text/plain; charset=UTF-8");
    response.setEntity(entity);
  } catch (UnsupportedEncodingException e) {
  }
}

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

@Override
protected void handleClientRequest(String absPath, Map<String, String> queryItems, HttpResponse response,
    Address serverAddressAtClient) throws IOException {
  // Individual info request
  String infoResponse = handleInfoRequest(absPath, queryItems, response);
  if (infoResponse == null) { // error condition, handleInfoRequest has set an error message
    return;
  }
  response.setStatusCode(HttpStatus.SC_OK);
  try {
    NStringEntity entity = new NStringEntity(infoResponse, "UTF-8");
    entity.setContentType("text/plain; charset=UTF-8");
    response.setEntity(entity);
  } catch (UnsupportedEncodingException e) {
  }
}

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

response.setStatusCode(HttpStatus.SC_OK);
  return;
} else { // not streaming audio

相关文章