當前位置: 首頁>>代碼示例>>Java>>正文


Java HttpURLConnection.HTTP_RESET屬性代碼示例

本文整理匯總了Java中java.net.HttpURLConnection.HTTP_RESET屬性的典型用法代碼示例。如果您正苦於以下問題:Java HttpURLConnection.HTTP_RESET屬性的具體用法?Java HttpURLConnection.HTTP_RESET怎麽用?Java HttpURLConnection.HTTP_RESET使用的例子?那麽, 這裏精選的屬性代碼示例或許可以為您提供幫助。您也可以進一步了解該屬性所在java.net.HttpURLConnection的用法示例。


在下文中一共展示了HttpURLConnection.HTTP_RESET屬性的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: 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
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:47,代碼來源:ResponseCacheTest.java

示例2: 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
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:50,代碼來源:CacheTest.java

示例3: 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
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:48,代碼來源:UrlConnectionCacheTest.java

示例4: inspect

public void inspect() throws IOException {
    final BlockInfo blockInfo = info.getBlock(blockIndex);
    boolean isServerCancelled = false;
    ResumeFailedCause resumeFailedCause = null;

    final int code = connected.getResponseCode();
    final String etag = info.getEtag();
    final String newEtag = connected.getResponseHeaderField("Etag");

    do {
        if (code == HttpURLConnection.HTTP_PRECON_FAILED) {
            resumeFailedCause = RESPONSE_PRECONDITION_FAILED;
            break;
        }

        if (!Util.isEmpty(etag) && !Util.isEmpty(newEtag) && !newEtag.equals(etag)) {
            // etag changed.
            // also etag changed is relate to HTTP_PRECON_FAILED
            resumeFailedCause = RESPONSE_ETAG_CHANGED;
            break;
        }

        if (code == HttpURLConnection.HTTP_CREATED && blockInfo.getCurrentOffset() != 0) {
            // The request has been fulfilled and has resulted in one or more new resources
            // being created.
            // mark this case is precondition failed for
            // 1. checkout whether accept partial
            // 2. 201 means new resources so range must be from beginning otherwise it can't
            // match local range.
            resumeFailedCause = RESPONSE_CREATED_RANGE_NOT_FROM_0;
            break;
        }

        if (code == HttpURLConnection.HTTP_RESET && blockInfo.getCurrentOffset() != 0) {
            resumeFailedCause = RESPONSE_RESET_RANGE_NOT_FROM_0;
            break;
        }

        if (code != HttpURLConnection.HTTP_PARTIAL && code != HttpURLConnection.HTTP_OK) {
            isServerCancelled = true;
            break;
        }

        if (code == HttpURLConnection.HTTP_OK && blockInfo.getCurrentOffset() != 0) {
            isServerCancelled = true;
            break;
        }
    } while (false);

    if (resumeFailedCause != null) {
        // resume failed, relaunch from beginning.
        throw new ResumeFailedException(resumeFailedCause);
    }

    if (isServerCancelled) {
        // server cancelled, end task.
        throw new ServerCancelledException(code, blockInfo.getCurrentOffset());
    }
}
 
開發者ID:lingochamp,項目名稱:okdownload,代碼行數:59,代碼來源:DownloadStrategy.java


注:本文中的java.net.HttpURLConnection.HTTP_RESET屬性示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。