当前位置: 首页>>代码示例>>Java>>正文


Java HttpURLConnection.HTTP_BAD_REQUEST属性代码示例

本文整理汇总了Java中java.net.HttpURLConnection.HTTP_BAD_REQUEST属性的典型用法代码示例。如果您正苦于以下问题:Java HttpURLConnection.HTTP_BAD_REQUEST属性的具体用法?Java HttpURLConnection.HTTP_BAD_REQUEST怎么用?Java HttpURLConnection.HTTP_BAD_REQUEST使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在java.net.HttpURLConnection的用法示例。


在下文中一共展示了HttpURLConnection.HTTP_BAD_REQUEST属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: readAscii

/**
 * Reads {@code count} characters from the stream. If the stream is exhausted before {@code count}
 * characters can be read, the remaining characters are returned and the stream is closed.
 */
private String readAscii(URLConnection connection, int count) throws IOException {
  HttpURLConnection httpConnection = (HttpURLConnection) connection;
  InputStream in = httpConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST
      ? connection.getInputStream()
      : httpConnection.getErrorStream();
  StringBuilder result = new StringBuilder();
  for (int i = 0; i < count; i++) {
    int value = in.read();
    if (value == -1) {
      in.close();
      break;
    }
    result.append((char) value);
  }
  return result.toString();
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:20,代码来源:UrlConnectionCacheTest.java

示例2: readAscii

/**
 * Reads {@code count} characters from the stream. If the stream is exhausted before {@code count}
 * characters can be read, the remaining characters are returned and the stream is closed.
 */
private String readAscii(URLConnection connection, int count) throws IOException {
  HttpURLConnection httpConnection = (HttpURLConnection) connection;
  InputStream in = httpConnection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST
      ? connection.getInputStream() : httpConnection.getErrorStream();
  StringBuilder result = new StringBuilder();
  for (int i = 0; i < count; i++) {
    int value = in.read();
    if (value == -1) {
      in.close();
      break;
    }
    result.append((char) value);
  }
  return result.toString();
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:19,代码来源:ResponseCacheTest.java

示例3: handleExchange

private void handleExchange(HttpExchange exchange) throws IOException {
  exchanges.add(exchange);
  requestBodies.add(streamToString(exchange.getRequestBody()));

  int status = getResponseType(exchange);
  byte[] response = null;
  switch (status) {
    case HttpURLConnection.HTTP_BAD_REQUEST:
      response = STATUS400_RESPONSE_BODY.getBytes();
      break;
    default:
      response = SUCCESS_RESPONSE_BODY.getBytes();
      status = HttpURLConnection.HTTP_OK;
      break;
  }
  exchange.sendResponseHeaders(status, response.length);
  exchange.getResponseBody().write(response);
  exchange.close();
}
 
开发者ID:ApptuitAI,项目名称:JInsight,代码行数:19,代码来源:ApptuitPutClientTest.java

示例4: make

private static Take make(final Take take) {
    return new TkFallback(
            take,
            new FbChain(
                    new FbStatus(
                            HttpURLConnection.HTTP_NOT_FOUND,
                            new RsWithStatus(
                                    new RsText("endpoint with this path not found"),
                                    HttpURLConnection.HTTP_NOT_FOUND
                            )
                    ),
                    new FbStatus(
                            HttpURLConnection.HTTP_BAD_REQUEST,
                            new RsWithStatus(
                                    new RsText("bad request"),
                                    HttpURLConnection.HTTP_BAD_REQUEST
                            )
                    ),
                    req -> new Opt.Empty<>(),
                    req -> new Opt.Single<>(fatal(req))
            )
    );
}
 
开发者ID:yaroska,项目名称:true_oop,代码行数:23,代码来源:TkAppFallback.java

示例5: readStreamBytes

private static byte[] readStreamBytes(HttpURLConnection connection) throws IOException {
    InputStream inputStream;
    if (connection.getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST) {
        inputStream = connection.getInputStream();
    } else {
        inputStream = connection.getErrorStream();
    }
    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

    String lines;
    StringBuilder sb = new StringBuilder("");
    while ((lines = reader.readLine()) != null) {
        lines = new String(lines.getBytes(), _charset);
        sb.append(lines);
    }
    reader.close();

    return sb.toString().getBytes(_charset);
}
 
开发者ID:MalongTech,项目名称:productai-java-sdk,代码行数:19,代码来源:RequestHelper.java

示例6: doPost

private static InputStream doPost(URL url, Map<String, String> headers, byte[] data, Response res) throws IOException {
    HttpURLConnection http = (HttpURLConnection) url.openConnection();
    http.setRequestMethod("POST");
    http.setRequestProperty("Content-Type", "application/json");
    if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            http.setRequestProperty(entry.getKey(), entry.getValue());
        }
    }
    http.setDoOutput(true);

    http.getOutputStream().write(data);
    http.getOutputStream().flush();

    int resCode = (http.getResponseCode());
    res.setCode(resCode);

    return (resCode < HttpURLConnection.HTTP_BAD_REQUEST)
            ? http.getInputStream() : http.getErrorStream();
}
 
开发者ID:sherisaac,项目名称:TurteTracker_APIServer,代码行数:20,代码来源:TestClient.java

示例7: validateHeaders

/**
 * Examines the headers in the response and throws an exception if appropriate.
 **/
private void validateHeaders( WebResponse response ) throws HttpException {
    if (!getExceptionsThrownOnErrorStatus()) return;

    if (response.getHeaderField( "WWW-Authenticate" ) != null) {
        throw new AuthorizationRequiredException( response.getHeaderField( "WWW-Authenticate" ) );
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
        throw new HttpInternalErrorException( response.getURL() );
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new HttpNotFoundException( response.getResponseMessage(), response.getURL() );
    } else if (response.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() );
    }
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:16,代码来源:WebClient.java

示例8: getInputStreamFromConnection

private InputStream getInputStreamFromConnection(HttpURLConnection connection) throws IOException {
	int responseCode = connection.getResponseCode();

	if (responseCode == HttpURLConnection.HTTP_FORBIDDEN || responseCode == HttpURLConnection.HTTP_BAD_REQUEST) {
		return connection.getErrorStream();
	} else if (responseCode == HttpURLConnection.HTTP_OK) {
		return connection.getInputStream();
	}

	return null;
}
 
开发者ID:kawaiiDango,项目名称:pScrobbler,代码行数:11,代码来源:Caller.java

示例9: ServletUnitWebResponse

/**
 * Constructs a response object from a servlet response.
 * @param target the target frame on which the response will be displayed
 * @param url the url from which the response was received
 * @param response the response populated by the servlet
 **/
ServletUnitWebResponse( ServletUnitClient client, String target, URL url, HttpServletResponse response, boolean throwExceptionOnError ) throws IOException {
    super( client, target, url );
    _response = (ServletUnitHttpResponse) response;
    /** make sure that any IO exception for HTML received page happens here, not later. **/
    if (getResponseCode() < HttpURLConnection.HTTP_BAD_REQUEST || !throwExceptionOnError) {
        defineRawInputStream( new ByteArrayInputStream( _response.getContents() ) );
        if (getContentType().startsWith( "text" )) loadResponseText();
    }
}
 
开发者ID:parabuild-ci,项目名称:parabuild-ci,代码行数:15,代码来源:ServletUnitWebResponse.java

示例10: getResponseType

private int getResponseType(HttpExchange exchange) {
  URI uri = exchange.getRequestURI();
  String rawQuery = uri.getRawQuery();
  if (rawQuery == null) {
    return HttpURLConnection.HTTP_OK;
  }
  if ("status=400".equals(rawQuery)) {
    return HttpURLConnection.HTTP_BAD_REQUEST;
  }
  return HttpURLConnection.HTTP_OK;
}
 
开发者ID:ApptuitAI,项目名称:JInsight,代码行数:11,代码来源:ApptuitPutClientTest.java

示例11: doDelete

private static InputStream doDelete(URL url, Map<String, String> headers, Response res) throws IOException {
    HttpURLConnection http = (HttpURLConnection) url.openConnection();
    http.setRequestMethod("DELETE");
    if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            http.setRequestProperty(entry.getKey(), entry.getValue());
        }
    }
    int resCode = (http.getResponseCode());
    res.setCode(resCode);

    return (resCode < HttpURLConnection.HTTP_BAD_REQUEST)
            ? http.getInputStream() : http.getErrorStream();
}
 
开发者ID:sherisaac,项目名称:TurteTracker_APIServer,代码行数:14,代码来源:TestClient.java

示例12: validateHeaders

/**
 * Examines the headers in the response and throws an exception if appropriate.
 * @parm response - the response to validate
 **/
private void validateHeaders( WebResponse response ) throws HttpException {
    if (!getExceptionsThrownOnErrorStatus()) 
    	return;
    // see feature request [ 914314 ] Add HttpException.getResponse for better reporting
    // for possible improvements here
    if (response.getResponseCode() == HttpURLConnection.HTTP_INTERNAL_ERROR) {
        throw new HttpInternalErrorException( response.getURL() );
    } else if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new HttpNotFoundException( response.getResponseMessage(), response.getURL() );
    } else if (response.getResponseCode() >= HttpURLConnection.HTTP_BAD_REQUEST) {
        throw new HttpException( response.getResponseCode(), response.getResponseMessage(), response.getURL() );
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:17,代码来源:WebClient.java

示例13: HttpWebResponse

/**
 * Constructs a response object from an input stream.
 * @param frame the target window or frame to which the request should be directed
 * @param url the url from which the response was received
 * @param connection the URL connection from which the response can be read
 **/
HttpWebResponse( WebConversation client, FrameSelector frame, URL url, URLConnection connection, boolean throwExceptionOnError ) throws IOException {
    super( client, frame, url );
    if (HttpUnitOptions.isLoggingHttpHeaders()) System.out.println( "\nReceived from " + url );
    readHeaders( connection );

    /** make sure that any IO exception for HTML received page happens here, not later. **/
    if (_responseCode < HttpURLConnection.HTTP_BAD_REQUEST || !throwExceptionOnError) {
        defineRawInputStream( new BufferedInputStream( getInputStream( connection ) ) );
        if (getContentType().startsWith( "text" )) loadResponseText();
    }
}
 
开发者ID:lamsfoundation,项目名称:lams,代码行数:17,代码来源:HttpWebResponse.java

示例14: processError

/**
 *  Processes an HTTP error condition, sending an error response to
 *  the client.
 *
 * @param code the error condition code
 */
private void processError(int code) {

    String msg;

    server.printWithThread("processError " + code);

    switch (code) {

        case HttpURLConnection.HTTP_BAD_REQUEST :
            msg = getHead(HEADER_BAD_REQUEST, false, null, 0);
            msg += ResourceBundleHandler.getString(
                WebServer.webBundleHandle, "BAD_REQUEST");
            break;

        case HttpURLConnection.HTTP_FORBIDDEN :
            msg = getHead(HEADER_FORBIDDEN, false, null, 0);
            msg += ResourceBundleHandler.getString(
                WebServer.webBundleHandle, "FORBIDDEN");
            break;

        case HttpURLConnection.HTTP_NOT_FOUND :
        default :
            msg = getHead(HEADER_NOT_FOUND, false, null, 0);
            msg += ResourceBundleHandler.getString(
                WebServer.webBundleHandle, "NOT_FOUND");
            break;
    }

    try {
        OutputStream os =
            new BufferedOutputStream(socket.getOutputStream());

        os.write(msg.getBytes(ENCODING));
        os.flush();
        os.close();
    } catch (Exception e) {
        server.printError("processError: " + e.toString());
        server.printStackTrace(e);
    }
}
 
开发者ID:tiweGH,项目名称:OpenDiabetes,代码行数:46,代码来源:WebServerConnection.java

示例15: processError

/**
 *  Processess an HTTP error condition, sending an error response to
 *  the client.
 *
 * @param code the error condition code
 */
private void processError(int code) {

    String msg;

    server.printWithThread("processError " + code);

    switch (code) {

        case HttpURLConnection.HTTP_BAD_REQUEST :
            msg = getHead(HEADER_BAD_REQUEST, false, null, 0);
            msg += ResourceBundleHandler.getString(
                WebServer.webBundleHandle, "BAD_REQUEST");
            break;

        case HttpURLConnection.HTTP_FORBIDDEN :
            msg = getHead(HEADER_FORBIDDEN, false, null, 0);
            msg += ResourceBundleHandler.getString(
                WebServer.webBundleHandle, "FORBIDDEN");
            break;

        case HttpURLConnection.HTTP_NOT_FOUND :
        default :
            msg = getHead(HEADER_NOT_FOUND, false, null, 0);
            msg += ResourceBundleHandler.getString(
                WebServer.webBundleHandle, "NOT_FOUND");
            break;
    }

    try {
        OutputStream os =
            new BufferedOutputStream(socket.getOutputStream());

        os.write(msg.getBytes(ENCODING));
        os.flush();
        os.close();
    } catch (Exception e) {
        server.printError("processError: " + e.toString());
        server.printStackTrace(e);
    }
}
 
开发者ID:Julien35,项目名称:dev-courses,代码行数:46,代码来源:WebServerConnection.java


注:本文中的java.net.HttpURLConnection.HTTP_BAD_REQUEST属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。