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


Java HttpURLConnection.HTTP_PARTIAL屬性代碼示例

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


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

示例1: connect

/** Use HTTP Range header for specifying offset. */
@Override
protected HttpURLConnection connect(final long offset,
    final boolean resolved) throws IOException {
  final HttpURLConnection conn = openConnection();
  conn.setRequestMethod("GET");
  if (offset != 0L) {
    conn.setRequestProperty("Range", "bytes=" + offset + "-");
  }
  conn.connect();

  //Expects HTTP_OK or HTTP_PARTIAL response codes.
  final int code = conn.getResponseCode();
  if (offset != 0L && code != HttpURLConnection.HTTP_PARTIAL) {
    throw new IOException("HTTP_PARTIAL expected, received " + code);
  } else if (offset == 0L && code != HttpURLConnection.HTTP_OK) {
    throw new IOException("HTTP_OK expected, received " + code);
  }
  return conn;
}
 
開發者ID:naver,項目名稱:hadoop,代碼行數:20,代碼來源:HftpFileSystem.java

示例2: initNetworkConnect

@Override
protected void initNetworkConnect() throws Exception {
    /** Create http connection. */
    URL url = new URL(task.getUrlStr());
    httpURLConnection = (HttpURLConnection) url.openConnection();

    /** Add header. */
    addHeader();

    /** Response 206 or 200, download. */
    int responseCode = httpURLConnection.getResponseCode();
    if (responseCode != HttpURLConnection.HTTP_PARTIAL
            && responseCode != HttpURLConnection.HTTP_OK) {
        throw new IOException("Server Error! responseCode : " + responseCode + ", url : " + url);
    }
}
 
開發者ID:xyhuangjinfu,項目名稱:MiniDownloader,代碼行數:16,代碼來源:HttpWorker.java

示例3: isSplitBlock

public boolean isSplitBlock(final long contentLength,
                            @NonNull DownloadConnection.Connected connected) throws
        IOException {
    // chunked
    if (contentLength == CHUNKED_CONTENT_LENGTH) return false;

    // output stream not support seek
    if (!OkDownload.with().outputStreamFactory().supportSeek()) return false;

    // partial, support range
    return connected.getResponseCode() == HttpURLConnection.HTTP_PARTIAL;
}
 
開發者ID:lingochamp,項目名稱:okdownload,代碼行數:12,代碼來源:DownloadStrategy.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

示例5: getHttpStatusCode

@Override
public int getHttpStatusCode() {
    int httpStatusCode = mHttpStatusCode;

    // If we have been able to successfully resume a previously interrupted
    // download,
    // the status code will be 206, not 200. Since the rest of the
    // application is
    // expecting 200 to indicate success, we need to fake it.
    if (httpStatusCode == HttpURLConnection.HTTP_PARTIAL) {
        httpStatusCode = HttpURLConnection.HTTP_OK;
    }
    return httpStatusCode;
}
 
開發者ID:lizhangqu,項目名稱:chromium-net-for-android,代碼行數:14,代碼來源:HttpUrlConnectionUrlRequest.java

示例6: run

@Override
public void run() {
    mIsRunning = true;
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    // 1.檢測是否存在未下載完的文件
    _checkIsResumeAvailable();

    Response response = null;
    Call call = null;
    try {
        // 2.構建請求
        Request.Builder requestBuilder = new Request.Builder().url(mFileInfo.getUrl());
        requestBuilder.addHeader("Range", String.format(Locale.ENGLISH, "bytes=%d-", mFileInfo.getLoadBytes()));
        // 目前沒有指定cache,下載任務非普通REST請求,用戶已經有了存儲的地方
        requestBuilder.cacheControl(CacheControl.FORCE_NETWORK);

        // 3.初始化請求
        Request request = requestBuilder.build();
        call = mClient.newCall(request);

        // 4.執行請求
        response = call.execute();

        final boolean isSucceedStart = response.code() == HttpURLConnection.HTTP_OK;
        final boolean isSucceedResume = response.code() == HttpURLConnection.HTTP_PARTIAL;

        if (isSucceedResume || isSucceedStart) {
            int total = mFileInfo.getTotalBytes();
            final String transferEncoding = response.header("Transfer-Encoding");

            // 5.獲取文件長度
            if (isSucceedStart || total <= 0) {
                if (transferEncoding == null) {
                    total = (int) response.body().contentLength();
                } else {
                    // if transfer not nil, ignore content-length
                    total = -1;
                }
                mFileInfo.setTotalBytes(total);
            }
            if (total < 0) {
                throw new DownloadException("Get content length error!");
            }

            // 6.網絡狀態已連接
            _onConnected();
            // 7.開始獲取數據
            _onDownload(response);
        } else {
            if (!_onRetry()) {
                mListener.onError(mFileInfo, "Numeric status code is error!");
                _updateDb();
            }
        }
    } catch (Throwable e) {
        if (e instanceof DownloadException) {
            if (_onRetry()) {
                return;
            }
        }
        mListener.onError(mFileInfo, e.toString());
        _updateDb();
    } finally {
        if (response != null && response.body() != null) {
            response.body().close();
        }
        if (call != null) {
            call.cancel();
        }
    }
}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:72,代碼來源:DownloadTask.java

示例7: run

@Override
public void run() {
    Process.setThreadPriority(Process.THREAD_PRIORITY_BACKGROUND);

    FileDownloadConnection connection = null;
    final long beginOffset = connectTask.getProfile().currentOffset;
    boolean isConnected = false;
    do {

        try {
            if (paused) {
                return;
            }

            isConnected = false;
            connection = connectTask.connect();
            final int code = connection.getResponseCode();

            if (FileDownloadLog.NEED_LOG) {
                FileDownloadLog.d(this, "the connection[%d] for %d, is connected %s with code[%d]",
                        connectionIndex, downloadId, connectTask.getProfile(), code);
            }

            if (code != HttpURLConnection.HTTP_PARTIAL && code != HttpURLConnection.HTTP_OK) {
                throw new SocketException(FileDownloadUtils.
                        formatString("Connection failed with request[%s] response[%s] http-state[%d] on task[%d-%d], " +
                                        "which is changed after verify connection, so please try again.",
                                connectTask.getRequestHeader(), connection.getResponseHeaderFields(),
                                code, downloadId, connectionIndex));
            }

            isConnected = true;
            final FetchDataTask.Builder builder = new FetchDataTask.Builder();

            if (paused) return;
            fetchDataTask = builder
                    .setDownloadId(downloadId)
                    .setConnectionIndex(connectionIndex)
                    .setCallback(callback)
                    .setHost(this)
                    .setWifiRequired(isWifiRequired)
                    .setConnection(connection)
                    .setConnectionProfile(this.connectTask.getProfile())
                    .setPath(path)
                    .build();


            fetchDataTask.run();
            if (paused){
                fetchDataTask.pause();
            }
            break;
        } catch (IllegalAccessException | IOException | FileDownloadGiveUpRetryException | IllegalArgumentException e) {
            if (callback.isRetry(e)) {
                if (!isConnected) {
                    callback.onRetry(e, 0);
                } else if (fetchDataTask != null) {
                    // connected
                    final long invalidIncreaseBytes = fetchDataTask.currentOffset - beginOffset;
                    callback.onRetry(e, invalidIncreaseBytes);
                } else {
                    // connected but create fetch data task failed, give up directly.
                    FileDownloadLog.w(this, "it is valid to retry and connection is valid but" +
                            " create fetch-data-task failed, so give up directly with %s", e);
                    callback.onError(e);
                    break;
                }

            } else {
                callback.onError(e);
                break;
            }

        } finally {
            if (connection != null) connection.ending();
        }
    } while (true);

}
 
開發者ID:weiwenqiang,項目名稱:GitHub,代碼行數:79,代碼來源:DownloadRunnable.java

示例8: setResumeSupportedOrNot

private void setResumeSupportedOrNot() {
    isResumeSupported = (responseCode == HttpURLConnection.HTTP_PARTIAL);
}
 
開發者ID:MindorksOpenSource,項目名稱:PRDownloader,代碼行數:3,代碼來源:DownloadTask.java


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