private static String execute0(HttpUriRequest httpUriRequest) throws Exception { CloseableHttpResponse closeableHttpResponse = null; String var4; try { closeableHttpResponse = closeableHttpClient.execute(httpUriRequest); String response = EntityUtils.toString(closeableHttpResponse.getEntity(), CHARSET); var4 = response; } catch (Exception var7) { throw var7; } finally { CloseUtils.close(closeableHttpResponse); } return var4; }
@Override public Proxy determineProxy(HttpHost host, HttpRequest request, HttpContext context, IPPool ipPool, CrawlerSession crawlerSession) { HttpClientContext httpClientContext = HttpClientContext.adapt(context); Proxy proxy = (Proxy) crawlerSession.getExtInfo(VSCRAWLER_AVPROXY_KEY); if (proxy == null) { String accessUrl = null; if (request instanceof HttpRequestWrapper || request instanceof HttpGet) { accessUrl = HttpUriRequest.class.cast(request).getURI().toString(); } if (!PoolUtil.isDungProxyEnabled(httpClientContext)) { log.info("{}不会被代理", accessUrl); return null; } proxy = ipPool.getIP(host.getHostName(), accessUrl); if (proxy == null) { return null; } crawlerSession.setExtInfo(VSCRAWLER_AVPROXY_KEY, proxy); } return proxy; }
public <T> Response<T> execute() { HttpProxy httpProxy = getHttpProxyFromPool(); CookieStore cookieStore = getCookieStoreFromPool(); CloseableHttpClient httpClient = httpClientPool.getHttpClient(siteConfig, request); HttpUriRequest httpRequest = httpClientPool.createHttpUriRequest(siteConfig, request, createHttpHost(httpProxy)); CloseableHttpResponse httpResponse = null; IOException executeException = null; try { HttpContext httpContext = createHttpContext(httpProxy, cookieStore); httpResponse = httpClient.execute(httpRequest, httpContext); } catch (IOException e) { executeException = e; } Response<T> response = ResponseFactory.createResponse( request.getResponseType(), siteConfig.getCharset(request.getUrl())); response.handleHttpResponse(httpResponse, executeException); return response; }
/** * * @param mch_id mch_id * @param request request * @param clazz clazz * @param sign_type 数据返回验证签名类型 * @param key 数据返回验证签名key * @since 2.8.5 * @return result */ public static <T> T keyStoreExecuteXmlResult(String mch_id,HttpUriRequest request,Class<T> clazz,String sign_type,String key){ String uriId = loggerRequest(request); ResponseHandler<T> responseHandler = XmlResponseHandler.createResponseHandler(clazz,sign_type,key); if(responseHandler instanceof LocalResponseHandler){ LocalResponseHandler lrh = (LocalResponseHandler) responseHandler; lrh.setUriId(uriId); } try { T t = httpClient_mchKeyStore.get(mch_id).execute(request,responseHandler,HttpClientContext.create()); if(resultErrorHandler != null){ resultErrorHandler.doHandle(uriId, request, t); } return t; } catch (Exception e) { e.printStackTrace(); logger.error(e.getMessage()); } return null; }
/** * Wraps an HttpClient.execute() to manage the authorization headers. * This will add the proper Authorization header, and retry if the * auth token has expired. */ @Override public HttpResponse execute(HttpUriRequest request) throws ClientProtocolException, IOException { initialize(); addGoogleAuthHeader(request, authToken); HttpResponse response = client.execute(request); if (response.getStatusLine().getStatusCode() == 401) { Log.i(LOG_TAG, "Invalid token: " + authToken); accountManager.invalidateAuthToken(ACCOUNT_TYPE, authToken); authToken = getAuthToken(); removeGoogleAuthHeaders(request); addGoogleAuthHeader(request, authToken); Log.i(LOG_TAG, "new token: " + authToken); response = client.execute(request); } return response; }
public void rawGet(String str, RawNetworkCallback rawNetworkCallback) throws Throwable { HttpUriRequest httpGet = new HttpGet(str); HttpClient sSLHttpClient = str.startsWith("https://") ? getSSLHttpClient() : new DefaultHttpClient(); HttpResponse execute = sSLHttpClient.execute(httpGet); int statusCode = execute.getStatusLine().getStatusCode(); if (statusCode == 200) { if (rawNetworkCallback != null) { rawNetworkCallback.onResponse(execute.getEntity().getContent()); } sSLHttpClient.getConnectionManager().shutdown(); return; } String entityUtils = EntityUtils.toString(execute.getEntity(), Constants.UTF_8); HashMap hashMap = new HashMap(); hashMap.put("error", entityUtils); hashMap.put("status", Integer.valueOf(statusCode)); sSLHttpClient.getConnectionManager().shutdown(); throw new Throwable(new Hashon().fromHashMap(hashMap)); }
@Nullable private CurseProject getModpack0(@Nonnull String slug) { try { log.debug("Getting modpack from server..."); URI uri = getURI(MCF_URL, String.format(PACK_PATH, slug), null); HttpUriRequest request = new HttpGet(uri.toURL().toString()); HttpResponse response = http.execute(request); String json = EntityUtils.toString(response.getEntity()); return GSON.fromJson(json, CurseProject.class); } catch (Exception e) { log.error("Failed to perform request from MCF Widget API.", e); return null; } }
public HttpClient(final Console console) { this.credentials = new HashMap<String, Credentials>(); this.request = new AtomicReference<>(); this.baseUrl = new Supplier<String>() { @Override public String get(Set<Hint> hints) { return console.getBaseUrl(); } }; this.httpClient = createHttpClient(); this.httpClientContext = createHttpClientContext(console); console.onInterrupt(new Runnable() { @Override public void run() { HttpUriRequest req = request.get(); if (req != null) { req.abort(); } } }); }
private HttpUriRequest constructRequest(byte[] pduBytes, boolean useProxy) throws IOException { try { HttpPostHC4 request = new HttpPostHC4(apn.getMmsc()); for (Header header : getBaseHeaders()) { request.addHeader(header); } request.setEntity(new ByteArrayEntityHC4(pduBytes)); if (useProxy) { HttpHost proxy = new HttpHost(apn.getProxy(), apn.getPort()); request.setConfig(RequestConfig.custom().setProxy(proxy).build()); } return request; } catch (IllegalArgumentException iae) { throw new IOException(iae); } }
@Nullable private String getModSlug0(int id) { try { log.debug("Getting mod slug from server..."); URI uri = getURI(CURSEFORGE_URL, String.format(PROJECT_PATH, id), null); HttpGet request = new HttpGet(uri.toURL().toString()); HttpContext context = new BasicHttpContext(); HttpResponse response = http.execute(request, context); EntityUtils.consume(response.getEntity()); HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(HttpCoreContext.HTTP_REQUEST); HttpHost currentHost = (HttpHost) context.getAttribute(HttpCoreContext.HTTP_TARGET_HOST); String currentUrl = (currentReq.getURI().isAbsolute()) ? currentReq.getURI().toString() : (currentHost.toURI() + currentReq.getURI()); Splitter splitter = Splitter.on('/').omitEmptyStrings(); List<String> pathParts = splitter.splitToList(currentUrl); return pathParts.get(pathParts.size() - 1); } catch (Exception e) { log.error("Failed to perform request from CurseForge site.", e); return null; } }
public void testResponseWarnings() throws Exception { HttpHost host = new HttpHost("localhost", 9200); HttpUriRequest request = randomHttpRequest(new URI("/index/type/_api")); int numWarnings = randomIntBetween(1, 5); StringBuilder expected = new StringBuilder("request [").append(request.getMethod()).append(" ").append(host) .append("/index/type/_api] returned ").append(numWarnings).append(" warnings: "); Header[] warnings = new Header[numWarnings]; for (int i = 0; i < numWarnings; i++) { String warning = "this is warning number " + i; warnings[i] = new BasicHeader("Warning", warning); if (i > 0) { expected.append(","); } expected.append("[").append(warning).append("]"); } assertEquals(expected.toString(), RequestLogger.buildWarningMessage(request, host, warnings)); }
@Before @SuppressWarnings("unchecked") public void createRestClient() throws IOException { CloseableHttpAsyncClient httpClient = mock(CloseableHttpAsyncClient.class); when(httpClient.<HttpResponse>execute(any(HttpAsyncRequestProducer.class), any(HttpAsyncResponseConsumer.class), any(HttpClientContext.class), any(FutureCallback.class))).thenAnswer(new Answer<Future<HttpResponse>>() { @Override public Future<HttpResponse> answer(InvocationOnMock invocationOnMock) throws Throwable { HttpAsyncRequestProducer requestProducer = (HttpAsyncRequestProducer) invocationOnMock.getArguments()[0]; HttpUriRequest request = (HttpUriRequest)requestProducer.generateRequest(); HttpHost httpHost = requestProducer.getTarget(); HttpClientContext context = (HttpClientContext) invocationOnMock.getArguments()[2]; assertThat(context.getAuthCache().get(httpHost), instanceOf(BasicScheme.class)); FutureCallback<HttpResponse> futureCallback = (FutureCallback<HttpResponse>) invocationOnMock.getArguments()[3]; //return the desired status code or exception depending on the path if (request.getURI().getPath().equals("/soe")) { futureCallback.failed(new SocketTimeoutException(httpHost.toString())); } else if (request.getURI().getPath().equals("/coe")) { futureCallback.failed(new ConnectTimeoutException(httpHost.toString())); } else if (request.getURI().getPath().equals("/ioe")) { futureCallback.failed(new IOException(httpHost.toString())); } else { int statusCode = Integer.parseInt(request.getURI().getPath().substring(1)); StatusLine statusLine = new BasicStatusLine(new ProtocolVersion("http", 1, 1), statusCode, ""); futureCallback.completed(new BasicHttpResponse(statusLine)); } return null; } }); int numHosts = RandomNumbers.randomIntBetween(getRandom(), 2, 5); httpHosts = new HttpHost[numHosts]; for (int i = 0; i < numHosts; i++) { httpHosts[i] = new HttpHost("localhost", 9200 + i); } failureListener = new HostsTrackingFailureListener(); restClient = new RestClient(httpClient, 10000, new Header[0], httpHosts, null, failureListener); }
void logRequest(String serviceName, HttpUriRequest request) throws IOException { System.setProperty("org.apache.commons.logging.Log","org.apache.commons.logging.impl.SimpleLog"); System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true"); System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.http.wire", "DEBUG"); CloseableHttpClient httpClient = signingClientForServiceName(serviceName); try (CloseableHttpResponse response = httpClient.execute(request)) { System.out.println(response.getStatusLine()); String inputLine ; BufferedReader br = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); try { while ((inputLine = br.readLine()) != null) { System.out.println(inputLine); } br.close(); } catch (IOException e) { e.printStackTrace(); } } }
private HttpUriRequest c() { if (this.f != null) { return this.f; } if (this.j == null) { byte[] b = this.c.b(); CharSequence b2 = this.c.b(AsyncHttpClient.ENCODING_GZIP); if (b != null) { if (TextUtils.equals(b2, "true")) { this.j = b.a(b); } else { this.j = new ByteArrayEntity(b); } this.j.setContentType(this.c.c()); } } HttpEntity httpEntity = this.j; if (httpEntity != null) { HttpUriRequest httpPost = new HttpPost(b()); httpPost.setEntity(httpEntity); this.f = httpPost; } else { this.f = new HttpGet(b()); } return this.f; }
@Override public BackendAuthResult authenticate(_MatrixID mxid, String password) { RestAuthRequestJson auth = new RestAuthRequestJson(); auth.setMxid(mxid.getId()); auth.setLocalpart(mxid.getLocalPart()); auth.setDomain(mxid.getDomain()); auth.setPassword(password); HttpUriRequest req = RestClientUtils.post(cfg.getEndpoints().getAuth(), gson, "auth", auth); try (CloseableHttpResponse res = client.execute(req)) { int status = res.getStatusLine().getStatusCode(); if (status < 200 || status >= 300) { return BackendAuthResult.failure(); } return parser.parse(res, "auth", BackendAuthResult.class); } catch (IOException e) { throw new RuntimeException(e); } }
/** * 拦截HttpClient的Post与Get方法. * */ @Around("execution(* org.apache.http.client.HttpClient.execute(..)) && args(httpUriRequest)") public Object around(final ProceedingJoinPoint proceedingJoinPoint, final HttpUriRequest httpUriRequest) throws Throwable { final long startTime = System.currentTimeMillis(); final Object[] args = proceedingJoinPoint.getArgs(); final Object result = proceedingJoinPoint.proceed(args); if (httpUriRequest instanceof HttpUriRequest) { final String methodName = httpUriRequest.getMethod(); final String className = httpUriRequest.getURI().toString(); Tracer.getInstance().addBinaryAnnotation(className, methodName, (int) (System.currentTimeMillis() - startTime)); } return result; }
private String getResponseInternal0(String baseUrl, String resourcePath) throws UnsupportedOperationException, IOException { CloseableHttpResponse response = null; String json = ""; try { HttpUriRequest request = new HttpGet(new URI(baseUrl + "/" + resourcePath)); response = client.execute(request); json = IOUtils.toString(response.getEntity().getContent()); } catch(Exception e) { LOGGER.error("Failed to execute request {} : {}",resourcePath,e); } finally { if(response != null) response.close(); } return json; }
@Test public void folder_can_fetch_sensible_body_data_and_notifies_listener() throws MessagingException, IOException, URISyntaxException { setupStoreForMessageFetching(); List<WebDavMessage> messages = setup25MessagesToFetch(); when(mockHttpClient.executeOverride(any(HttpUriRequest.class), any(HttpContext.class))).thenAnswer( new Answer<HttpResponse>() { @Override public HttpResponse answer(InvocationOnMock invocation) throws Throwable { HttpResponse httpResponse = mock(HttpResponse.class); StatusLine statusLine = mock(StatusLine.class); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(statusLine.getStatusCode()).thenReturn(200); BasicHttpEntity httpEntity = new BasicHttpEntity(); String body = ""; httpEntity.setContent(new ByteArrayInputStream(body.getBytes("UTF-8"))); when(httpResponse.getEntity()).thenReturn(httpEntity); return httpResponse; } }); FetchProfile profile = new FetchProfile(); profile.add(FetchProfile.Item.BODY_SANE); folder.fetch(messages, profile, listener); verify(listener, times(25)).messageStarted(any(String.class), anyInt(), eq(25)); verify(listener, times(25)).messageFinished(any(WebDavMessage.class), anyInt(), eq(25)); }
/** * 检验授权凭证(access_token)是否有效 * * @param access_token access_token * @param openid openid * @return result * @since 2.8.1 */ public static BaseResult auth(String access_token, String openid) { HttpUriRequest httpUriRequest = RequestBuilder.get() .setUri(BASE_URI + "/sns/auth") .addParameter(PARAM_ACCESS_TOKEN, API.accessToken(access_token)) .addParameter("openid", openid) .build(); return LocalHttpClient.executeJsonResult(httpUriRequest, BaseResult.class); }
/** * Generate a FusionTables POST request */ // To be deprecated, based on the old API private HttpUriRequest genFusiontablesQuery(String query) throws IOException { HttpPost request = new HttpPost(FUSION_QUERY_URL); ArrayList<BasicNameValuePair> pair = new ArrayList<BasicNameValuePair>(1); pair.add(new BasicNameValuePair("sql", query)); UrlEncodedFormEntity entity = new UrlEncodedFormEntity(pair, "UTF-8"); entity.setContentType("application/x-www-form-urlencoded"); request.setEntity(entity); return request; }
/** * レスポンスボディを受け取るGETメソッド. * @param url リクエスト対象URL * @param headers リクエストヘッダのハッシュマップ * @return DcResponse型 * @throws PersoniumException DAO例外 */ public final PersoniumResponse getAcceptEncodingGzip(final String url, final HashMap<String, String> headers) throws PersoniumException { HttpUriRequest req = new HttpGet(url); req.addHeader("Accept-Encoding", "gzip"); for (Map.Entry<String, String> entry : headers.entrySet()) { req.setHeader(entry.getKey(), entry.getValue()); } req.addHeader("X-Personium-Version", PersoniumCoreTestConfig.getCoreVersion()); debugHttpRequest(req, ""); PersoniumResponse res = this.request(req); return res; }
@Override public <T> T execute(final HttpUriRequest request, final ResponseHandler<? extends T> responseHandler, final HttpContext context) throws IOException, ClientProtocolException { Subsegment subsegment = recorder.beginSubsegment(determineTarget(request).getHostName()); return wrapHttpSupplier(subsegment, () -> { if (null != subsegment) { TracedHttpClient.addRequestInformation(subsegment, request, TracedHttpClient.getUrl(request)); } TracedResponseHandler<? extends T> wrappedHandler = new TracedResponseHandler<>(responseHandler); T response = wrappedClient.execute(request, wrappedHandler, context); return response; }); }
@Test public void createDeprecatedPostRequest() throws Exception { TestRequest.DeprecatedPost request = new TestRequest.DeprecatedPost(); assertEquals(request.getMethod(), Method.DEPRECATED_GET_OR_POST); HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null); assertTrue(httpRequest instanceof HttpPost); }
@Test public void createPostRequestWithBody() throws Exception { TestRequest.PostWithBody request = new TestRequest.PostWithBody(); assertEquals(request.getMethod(), Method.POST); HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null); assertTrue(httpRequest instanceof HttpPost); }
@Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { boolean retry = true; Boolean b = (Boolean) context.getAttribute(ExecutionContext.HTTP_REQ_SENT); boolean sent = (b != null && b); if (executionCount > maxRetries) { // Do not retry if over max retry count retry = false; } else if (isInList(exceptionWhitelist, exception)) { // immediately retry if error is whitelisted retry = true; } else if (isInList(exceptionBlacklist, exception)) { // immediately cancel retry if the error is blacklisted retry = false; } else if (!sent) { // for most other errors, retry only if request hasn't been fully sent yet retry = true; } if (retry) { // resend all idempotent requests HttpUriRequest currentReq = (HttpUriRequest) context.getAttribute(ExecutionContext.HTTP_REQUEST); if (currentReq == null) { return false; } } if (retry) { SystemClock.sleep(retrySleepTimeMS); } else { exception.printStackTrace(); } return retry; }
@Test public void createTraceRequest() throws Exception { TestRequest.Trace request = new TestRequest.Trace(); assertEquals(request.getMethod(), Method.TRACE); HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null); assertTrue(httpRequest instanceof HttpTrace); }
public final HttpResponse a(HttpUriRequest httpUriRequest) throws Exception { try { return this.c.execute(httpUriRequest); } catch (Exception e) { throw e; } }
@Test public void createPatchRequestWithBody() throws Exception { TestRequest.PatchWithBody request = new TestRequest.PatchWithBody(); assertEquals(request.getMethod(), Method.PATCH); HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null); assertTrue(httpRequest instanceof HttpPatch); }
@Override public HttpResponse execute(HttpUriRequest request, HttpContext context) { requestExecuted = request; StatusLine statusLine = new BasicStatusLine( new ProtocolVersion("HTTP", 1, 1), mStatusCode, ""); HttpResponse response = new BasicHttpResponse(statusLine); response.setEntity(mResponseEntity); return response; }
HttpComponentsAsyncClientHttpRequest(HttpAsyncClient httpClient, HttpUriRequest httpRequest, HttpContext httpContext) { this.httpClient = httpClient; this.httpRequest = httpRequest; this.httpContext = httpContext; }
public <T> CompletableFuture<HttpResponse<T>> requestAsync(HttpRequest request, final Class<T> responseClass) { HttpUriRequest requestObj = prepareRequest(request, true); if (!asyncClient.isRunning()) { asyncClient.start(); } CompletableFuture<HttpResponse<T>> completableFuture = new CompletableFuture<>(); asyncClient.execute(requestObj, prepareCallback(responseClass, new Callback<T>() { @Override public void completed(HttpResponse<T> response) { completableFuture.complete(response); } @Override public void failed(RestClientException e) { completableFuture.completeExceptionally(e); } @Override public void cancelled() { //do nothing } }, objectMapper)); return completableFuture; }
@Test public void createGetRequest() throws Exception { TestRequest.Get request = new TestRequest.Get(); assertEquals(request.getMethod(), Method.GET); HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null); assertTrue(httpRequest instanceof HttpGet); }
public RequestHandle get(Context context, String url, Header[] headers, RequestParams params, ResponseHandlerInterface responseHandler) { HttpUriRequest request = new HttpGet(getUrlWithQueryString(this.isUrlEncodingEnabled, url, params)); if (headers != null) { request.setHeaders(headers); } return sendRequest(this.httpClient, this.httpContext, request, null, responseHandler, context); }
public ContentItemsListingResponse getItems(List<NameValuePair> params) throws IOException { HttpUriRequest request = buildGetRequest(ITEMS, params); ContentItemsListingResponse contentItemsListingResponse = executeRequest(request, ContentItemsListingResponse.class); contentItemsListingResponse.setStronglyTypedContentItemConverter(stronglyTypedContentItemConverter); RichTextElementConverter converter = new RichTextElementConverter( getContentLinkUrlResolver(), getBrokenLinkUrlResolver(), getRichTextElementResolver(), templateEngineConfig, stronglyTypedContentItemConverter ); converter.process(contentItemsListingResponse.getItems()); return contentItemsListingResponse; }
/** * A helper method to convert an InputStream into a String * * @return the String or a blank string if the IS was null * @throws IOException */ public static String convertToString(String url) { if (url != null) { try { HttpClient client = new DefaultHttpClient(); client.getParams().setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (X11; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0"); HttpUriRequest request = new HttpGet(url); HttpResponse response = client.execute(request); BufferedReader in = new BufferedReader(new InputStreamReader( response.getEntity().getContent())); StringBuilder total = new StringBuilder(); String inputLine; while ((inputLine = in.readLine()) != null) { total.append(inputLine); } in.close(); return total.toString(); } catch (Exception e) { e.printStackTrace(); return ""; } } else { return ""; } }
@Test public void createDeleteRequest() throws Exception { TestRequest.Delete request = new TestRequest.Delete(); assertEquals(request.getMethod(), Method.DELETE); HttpUriRequest httpRequest = HttpClientStack.createHttpRequest(request, null); assertTrue(httpRequest instanceof HttpDelete); }
private static HttpHost determineTarget(HttpUriRequest request) throws ClientProtocolException { // A null target may be acceptable if there is a default target. // Otherwise, the null target is detected in the director. HttpHost target = null; URI requestURI = request.getURI(); if (requestURI.isAbsolute()) { target = URIUtils.extractHost(requestURI); if (target == null) { throw new ClientProtocolException( "URI does not specify a valid host name: " + requestURI); } } return target; }
public HttpUriRequest getRedirect( final HttpRequest request, final HttpResponse response, final HttpContext context) throws ProtocolException { URI uri = getLocationURI(request, response, context); String method = request.getRequestLine().getMethod(); if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) { return new HttpHead(uri); } else { return new HttpGet(uri); } }
@Override public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError { HttpUriRequest httpRequest = createHttpRequest(request, additionalHeaders); addHeaders(httpRequest, additionalHeaders); addHeaders(httpRequest, request.getHeaders()); onPrepareRequest(httpRequest); HttpParams httpParams = httpRequest.getParams(); int timeoutMs = request.getTimeoutMs(); // TODO: Reevaluate this connection timeout based on more wide-scale // data collection and possibly different for wifi vs. 3G. HttpConnectionParams.setConnectionTimeout(httpParams, 5000); HttpConnectionParams.setSoTimeout(httpParams, timeoutMs); return mClient.execute(httpRequest); }