本文整理汇总了Java中java.net.HttpURLConnection.HTTP_NO_CONTENT属性的典型用法代码示例。如果您正苦于以下问题:Java HttpURLConnection.HTTP_NO_CONTENT属性的具体用法?Java HttpURLConnection.HTTP_NO_CONTENT怎么用?Java HttpURLConnection.HTTP_NO_CONTENT使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类java.net.HttpURLConnection
的用法示例。
在下文中一共展示了HttpURLConnection.HTTP_NO_CONTENT属性的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: perform
@Override
public void perform(
final Command command, final Log log) throws IOException {
final String author = command.author();
final Request follow = command.issue().repo().github().entry()
.uri().path("/user/following/").path(author).back()
.method("PUT");
log.logger().info("Following Github user " + author + " ...");
try {
final int status = follow.fetch().status();
if(status != HttpURLConnection.HTTP_NO_CONTENT) {
log.logger().error(
"User follow status response is " + status
+ " . Should have been 204 (NO CONTENT)"
);
} else {
log.logger().info("Followed user " + author + " .");
}
} catch (final IOException ex) {
log.logger().warn("IOException while trying to follow the user.");
}
this.next().perform(command, log);
}
示例2: call
private boolean call(String methodURL){
try {
URL url = new URL(methodURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
String hmac = main.getPublicKey() + "." + main.hmac(methodURL);
connection.setRequestMethod("DELETE");
connection.setRequestProperty("User-Agent", "Pterodactyl Java-API");
connection.setRequestProperty("Authorization", "Bearer " + hmac.replaceAll("\n", ""));
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
return true;
} else {
this.lastError = main.readResponse(connection.getErrorStream()).toString();
return false;
}
} catch (Exception e) {
main.log(Level.SEVERE, e.getMessage());
e.printStackTrace();
return false;
}
}
示例3: assertCached
private void assertCached(boolean shouldPut, int responseCode) throws Exception {
int expectedResponseCode = responseCode;
server = new MockWebServer();
MockResponse mockResponse = new MockResponse()
.addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
.setResponseCode(responseCode)
.setBody("ABCDE")
.addHeader("WWW-Authenticate: challenge");
if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
mockResponse.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
mockResponse.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT
|| responseCode == HttpURLConnection.HTTP_RESET) {
mockResponse.setBody(""); // We forbid bodies for 204 and 205.
}
server.enqueue(mockResponse);
if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT) {
// 408's are a bit of an outlier because we may repeat the request if we encounter this
// response code. In this scenario, there are 2 responses: the initial 408 and then the 200
// because of the retry. We just want to ensure the initial 408 isn't cached.
expectedResponseCode = 200;
server.enqueue(new MockResponse()
.setHeader("Cache-Control", "no-store")
.setBody("FGHIJ"));
}
server.start();
URL url = server.url("/").url();
HttpURLConnection connection = openConnection(url);
assertEquals(expectedResponseCode, connection.getResponseCode());
// Exhaust the content stream.
readAscii(connection);
CacheResponse cached = cache.get(url.toURI(), "GET", null);
if (shouldPut) {
assertNotNull(Integer.toString(responseCode), cached);
} else {
assertNull(Integer.toString(responseCode), cached);
}
server.shutdown(); // tearDown() isn't sufficient; this test starts multiple servers
}
示例4: assertCached
private void assertCached(boolean shouldPut, int responseCode) throws Exception {
int expectedResponseCode = responseCode;
server = new MockWebServer();
MockResponse mockResponse = new MockResponse()
.addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
.setResponseCode(responseCode)
.setBody("ABCDE")
.addHeader("WWW-Authenticate: challenge");
if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
mockResponse.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
mockResponse.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT
|| responseCode == HttpURLConnection.HTTP_RESET) {
mockResponse.setBody(""); // We forbid bodies for 204 and 205.
}
server.enqueue(mockResponse);
if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT) {
// 408's are a bit of an outlier because we may repeat the request if we encounter this
// response code. In this scenario, there are 2 responses: the initial 408 and then the 200
// because of the retry. We just want to ensure the initial 408 isn't cached.
expectedResponseCode = 200;
server.enqueue(new MockResponse()
.setHeader("Cache-Control", "no-store")
.setBody("FGHIJ"));
}
server.start();
Request request = new Request.Builder()
.url(server.url("/"))
.build();
Response response = client.newCall(request).execute();
assertEquals(expectedResponseCode, response.code());
// Exhaust the content stream.
response.body().string();
Response cached = cache.get(request);
if (shouldPut) {
assertNotNull(Integer.toString(responseCode), cached);
cached.body().close();
} else {
assertNull(Integer.toString(responseCode), cached);
}
server.shutdown(); // tearDown() isn't sufficient; this test starts multiple servers
}
示例5: assertCached
private void assertCached(boolean shouldPut, int responseCode) throws Exception {
int expectedResponseCode = responseCode;
server = new MockWebServer();
MockResponse response = new MockResponse()
.addHeader("Last-Modified: " + formatDate(-1, TimeUnit.HOURS))
.addHeader("Expires: " + formatDate(1, TimeUnit.HOURS))
.setResponseCode(responseCode)
.setBody("ABCDE")
.addHeader("WWW-Authenticate: challenge");
if (responseCode == HttpURLConnection.HTTP_PROXY_AUTH) {
response.addHeader("Proxy-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_UNAUTHORIZED) {
response.addHeader("WWW-Authenticate: Basic realm=\"protected area\"");
} else if (responseCode == HttpURLConnection.HTTP_NO_CONTENT
|| responseCode == HttpURLConnection.HTTP_RESET) {
response.setBody(""); // We forbid bodies for 204 and 205.
}
server.enqueue(response);
if (responseCode == HttpURLConnection.HTTP_CLIENT_TIMEOUT) {
// 408's are a bit of an outlier because we may repeat the request if we encounter this
// response code. In this scenario, there are 2 responses: the initial 408 and then the 200
// because of the retry. We just want to ensure the initial 408 isn't cached.
expectedResponseCode = 200;
server.enqueue(new MockResponse()
.addHeader("Cache-Control", "no-store")
.setBody("FGHIJ"));
}
server.start();
URL url = server.url("/").url();
HttpURLConnection conn = urlFactory.open(url);
assertEquals(expectedResponseCode, conn.getResponseCode());
// exhaust the content stream
readAscii(conn);
Response cached = cache.get(new Request.Builder().url(url).build());
if (shouldPut) {
assertNotNull(Integer.toString(responseCode), cached);
cached.body().close();
} else {
assertNull(Integer.toString(responseCode), cached);
}
server.shutdown(); // tearDown() isn't sufficient; this test starts multiple servers
}
示例6: isConnected
protected Boolean isConnected(final String host, final int port, final int timeoutInMs,
final ErrorHandler errorHandler) {
HttpURLConnection urlConnection = null;
try {
urlConnection = createHttpUrlConnection(host, port, timeoutInMs);
return urlConnection.getResponseCode() == HttpURLConnection.HTTP_NO_CONTENT;
} catch (IOException e) {
errorHandler.handleError(e, "Could not establish connection with WalledGardenStrategy");
return Boolean.FALSE;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
示例7: act
@Override
public Response act(Request req) throws IOException {
long categoryId = Long.parseLong(((RqRegex) req).matcher().group("ctgId"));
long productId = Long.parseLong(((RqRegex) req).matcher().group("prdId"));
Category category = base.categories().category(categoryId);
Product product = base.products().product(productId);
base.categoryProducts(category).remove(product);
return new RsWithStatus(HttpURLConnection.HTTP_NO_CONTENT);
}
示例8: act
@Override
public Response act(Request req) throws IOException {
long categoryId = Long.parseLong(((RqRegex) req).matcher().group("number"));
Category category = base.categories().category(categoryId);
JsonReader reader = Json.createReader(req.body());
Product[] products = reader.readArray().stream()
.map(JsonValue::asJsonObject)
.map(product -> product.getInt("id"))
.map(id -> base.products().product(id))
.toArray(Product[]::new);
base.categoryProducts(category).add(products);
return new RsWithStatus(HttpURLConnection.HTTP_NO_CONTENT);
}
示例9: act
@Override
public Response act(Request req) throws IOException {
long categoryId = Long.parseLong(((RqRegex) req).matcher().group("ctgId"));
long productId = Long.parseLong(((RqRegex) req).matcher().group("prdId"));
Category category = base.categories().category(categoryId);
Product product = base.products().product(productId);
base.categoryProducts(category).add(product);
return new RsWithStatus(HttpURLConnection.HTTP_NO_CONTENT);
}
示例10: act
@Override
public Response act(Request req) throws IOException {
long number = Long.parseLong(((RqRegex) req).matcher().group("number"));
Product product = base.products().product(number);
base.products().delete(product);
return new RsWithStatus(HttpURLConnection.HTTP_NO_CONTENT);
}
示例11: act
@Override
public Response act(Request req) throws IOException {
long number = Long.parseLong(((RqRegex) req).matcher().group("number"));
Category category = base.categories().category(number);
base.categories().delete(category);
return new RsWithStatus(HttpURLConnection.HTTP_NO_CONTENT);
}
示例12: parseResponse
private AvsResponse parseResponse() throws IOException, AvsException, RuntimeException {
Request request = mRequestBuilder.build();
currentCall = ClientUtil.getTLS12OkHttpClient().newCall(request);
try {
Response response = currentCall.execute();
if(response.code() == HttpURLConnection.HTTP_NO_CONTENT){
Log.w(TAG, "Received a 204 response code from Amazon, is this expected?");
}
final AvsResponse val = response.code() == HttpURLConnection.HTTP_NO_CONTENT ? new AvsResponse() :
ResponseParser.parseResponse(response.body().byteStream(), getBoundary(response));
response.body().close();
return val;
} catch (IOException exp) {
if (!currentCall.isCanceled()) {
return new AvsResponse();
}
}
return null;
}
示例13: dispatch
@VisibleForTesting
public boolean dispatch(@NonNull final Packet packet) {
// Some error checking
if (packet.getTargetURL() == null)
return false;
if (packet.getJSONObject() != null && packet.getJSONObject().length() == 0)
return false;
if (mPiwik.isDryRun()) {
mDryRunOutput.add(packet);
Timber.tag(LOGGER_TAG).d("DryRun, stored HttpRequest, now %s.", mDryRunOutput.size());
return true;
}
if (!mDryRunOutput.isEmpty())
mDryRunOutput.clear();
try {
StrongOkHttpClientBuilder builder= StrongOkHttpClientBuilder.forMaxSecurity(mPiwik.getContext());
OkHttpClient client = null;
if (mCertPin != null) {
CertificatePinner certificatePinner = new CertificatePinner.Builder()
.add(packet.getTargetURL().getHost(), mCertPin)
.build();
client = new OkHttpClient.Builder()
.proxy(mProxy)
.certificatePinner(certificatePinner)
.build();
}
else
{
client = new OkHttpClient.Builder()
.proxy(mProxy)
.build();
}
Request request = null;
// IF there is json data we want to do a post
if (packet.getJSONObject() != null) {
MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
RequestBody body = RequestBody.create(JSON, packet.getJSONObject().toString());
request = new Request.Builder()
.url(packet.getTargetURL())
.post(body)
.build();
} else {
// GET
request = new Request.Builder()
.url(packet.getTargetURL())
.build();
}
Response response = client.newCall(request).execute();
int statusCode = response.code();
Timber.tag(LOGGER_TAG).d("status code %s", statusCode);
return statusCode == HttpURLConnection.HTTP_NO_CONTENT || statusCode == HttpURLConnection.HTTP_OK;
} catch (Exception e) {
// Broad but an analytics app shouldn't impact it's host app.
Timber.tag(LOGGER_TAG).w(e, "Cannot send request");
}
return false;
}