当前位置: 首页>>代码示例>>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;未经允许,请勿转载。