當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。