本文整理汇总了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();
}
示例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();
}
示例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();
}
示例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))
)
);
}
示例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);
}
示例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();
}
示例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() );
}
}
示例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;
}
示例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();
}
}
示例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;
}
示例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();
}
示例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() );
}
}
示例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();
}
}
示例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);
}
}
示例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);
}
}