Refine search
@Override public InputStream getInputStream() throws IOException { if (!doInput) { throw new ProtocolException("This protocol does not support input"); } Response response = getResponse(false); if (response.code() >= HTTP_BAD_REQUEST) { throw new FileNotFoundException(url.toString()); } return response.body().byteStream(); }
/** * Returns an input stream from the server in the case of error such as the requested file (txt, * htm, html) is not found on the remote server. */ @Override public InputStream getErrorStream() { try { Response response = getResponse(true); if (HttpHeaders.hasBody(response) && response.code() >= HTTP_BAD_REQUEST) { return response.body().byteStream(); } return null; } catch (IOException e) { return null; } }
private String retrieveAuthCode(OkHttpClient client) throws IOException { Request.Builder builder = new Request.Builder().url(authRetrievalUrl); try (Response authResponse = client.newCall(builder.build()).execute()) { ResponseBody authBody = authResponse.body(); if (authBody == null) { throw new AssertionError("AUTH ERROR: " + authResponse.code() + ":" + "<empty body>"); } String authCode = new String(authBody.bytes()); // System.out.println("AUTH: " + authResponse.code() + ":" + authCode); return authCode; } } }
@Override public Void call(String url) throws Exception { okhttp3.Request request = new okhttp3.Request.Builder() .post(RequestBody.create(null, new byte[0])) .url(url + "?level=" + newLogLevel.name()) .build(); try (okhttp3.Response response = new OkHttpClient().newCall(request).execute()) { if (response.code() != 200) { throw new IOException( String.format( "Failed to change log level in Compute Engine. Code was '%s' and response was '%s' for url '%s'", response.code(), response.body().string(), url)); } return null; } } }
public static <T> JsonResponse<T> execute(JsonCodec<T> codec, OkHttpClient client, Request request) { try (Response response = client.newCall(request).execute()) { // TODO: fix in OkHttp: https://github.com/square/okhttp/issues/3111 if ((response.code() == 307) || (response.code() == 308)) { String location = response.header(LOCATION); if (location != null) { request = request.newBuilder().url(location).build(); return execute(codec, client, request); } } ResponseBody responseBody = requireNonNull(response.body()); String body = responseBody.string(); if (isJson(responseBody.contentType())) { return new JsonResponse<>(response.code(), response.message(), response.headers(), body, codec); } return new JsonResponse<>(response.code(), response.message(), response.headers(), body); } catch (IOException e) { // OkHttp throws this after clearing the interrupt status // TODO: remove after updating to Okio 1.15.0+ if ((e instanceof InterruptedIOException) && "thread interrupted".equals(e.getMessage())) { Thread.currentThread().interrupt(); } throw new UncheckedIOException(e); } }
try (Response response = client.newCall(request).execute()) { String responseSource = response.networkResponse() != null ? ("(network: " + response.networkResponse().code() + " over " + response.protocol() + ")") : "(cache)"; int responseCode = response.code(); Document document = Jsoup.parse(response.body().string(), url.toString()); for (Element element : document.select("a[href]")) { String href = element.attr("href");
@Override public void onResponse(@NonNull Call call, @NonNull Response response) { responseBody = response.body(); if (response.isSuccessful()) { long contentLength = Preconditions.checkNotNull(responseBody).contentLength(); stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength); callback.onDataReady(stream); } else { callback.onLoadFailed(new HttpException(response.message(), response.code())); } }
/** * Gets object from Qiniu kodo. All requests are authenticated by default,default expires 3600s We * use okhttp as our HTTP client and support two main parameters in the external adjustment, MAX * request and timeout time. * @param key object key * @param startPos start index for object * @param endPos end index for object * @return inputstream * @param contentLength object file size * @throws IOException */ public InputStream getObject(String key, long startPos, long endPos, long contentLength) throws IOException { String baseUrl = String.format("http://%s/%s", mDownloadHost, key); String privateUrl = mAuth.privateDownloadUrl(baseUrl); URL url = new URL(privateUrl); String objectUrl = String.format("http://%s/%s?%s", mEndPoint, key, url.getQuery()); Request request = new Request.Builder().url(objectUrl) .addHeader("Range", "bytes=" + String.valueOf(startPos) + "-" + String.valueOf(endPos < contentLength ? endPos - 1 : contentLength - 1)) .addHeader("Host", mDownloadHost).get().build(); Response response = mOkHttpClient.newCall(request).execute(); if (response.code() != 200 && response.code() != 206) { throw new IOException(String.format("Qiniu kodo:get object failed errcode:%d,errmsg:%s", response.code(), response.message())); } return response.body().byteStream(); }
private ResponseBody getBody(String siteUrl, Map<String, String> customProperties) throws IOException, ReCaptchaException { final Request.Builder requestBuilder = new Request.Builder() .method("GET", null).url(siteUrl) .addHeader("User-Agent", USER_AGENT); for (Map.Entry<String, String> header : customProperties.entrySet()) { requestBuilder.addHeader(header.getKey(), header.getValue()); } if (!TextUtils.isEmpty(mCookies)) { requestBuilder.addHeader("Cookie", mCookies); } final Request request = requestBuilder.build(); final Response response = client.newCall(request).execute(); final ResponseBody body = response.body(); if (response.code() == 429) { throw new ReCaptchaException("reCaptcha Challenge requested"); } if (body == null) { response.close(); return null; } return body; }
private static HttpResponse transformResponse(Response response) { int code = response.code(); String message = response.message(); BasicHttpResponse httpResponse = new BasicHttpResponse(HTTP_1_1, code, message); ResponseBody body = response.body(); InputStreamEntity entity = new InputStreamEntity(body.byteStream(), body.contentLength()); httpResponse.setEntity(entity); Headers headers = response.headers(); for (int i = 0, size = headers.size(); i < size; i++) { String name = headers.name(i); String value = headers.value(i); httpResponse.addHeader(name, value); if ("Content-Type".equalsIgnoreCase(name)) { entity.setContentType(value); } else if ("Content-Encoding".equalsIgnoreCase(name)) { entity.setContentEncoding(value); } } return httpResponse; }
@Override public void onResponse(@NonNull Call call, @NonNull Response response) { responseBody = response.body(); if (response.isSuccessful()) { long contentLength = Preconditions.checkNotNull(responseBody).contentLength(); stream = ContentLengthInputStream.obtain(responseBody.byteStream(), contentLength); callback.onDataReady(stream); } else { callback.onLoadFailed(new HttpException(response.message(), response.code())); } }
@Override protected InputStream performIO(String request) throws IOException { RequestBody requestBody = RequestBody.create(JSON_MEDIA_TYPE, request); Headers headers = buildHeaders(); okhttp3.Request httpRequest = new okhttp3.Request.Builder() .url(url) .headers(headers) .post(requestBody) .build(); okhttp3.Response response = httpClient.newCall(httpRequest).execute(); ResponseBody responseBody = response.body(); if (response.isSuccessful()) { if (responseBody != null) { return buildInputStream(responseBody); } else { return null; } } else { int code = response.code(); String text = responseBody == null ? "N/A" : responseBody.string(); throw new ClientConnectionException("Invalid response received: " + code + "; " + text); } }
private List<InetAddress> readResponse(String hostname, Response response) throws Exception { if (response.cacheResponse() == null && response.protocol() != Protocol.HTTP_2) { Platform.get().log(Platform.WARN, "Incorrect protocol: " + response.protocol(), null); } try { if (!response.isSuccessful()) { throw new IOException("response: " + response.code() + " " + response.message()); } ResponseBody body = response.body(); if (body.contentLength() > MAX_RESPONSE_SIZE) { throw new IOException("response size exceeds limit (" + MAX_RESPONSE_SIZE + " bytes): " + body.contentLength() + " bytes"); } ByteString responseBytes = body.source().readByteString(); return DnsRecordCodec.decodeAnswers(hostname, responseBytes); } finally { response.close(); } }