BufferedReader in; HttpGet get = new HttpGet(location); get.setConfig(rc); try { response = httpclient.execute(get);
.setSocketTimeout(timeout) .build(); getMethod.setConfig(requestConfig); HttpResponse response = httpClient.execute(getMethod); int statusCode = response.getStatusLine().getStatusCode();
/** Get the AWS instance type if applicable */ public String getInstanceType () { try { HttpGet get = new HttpGet(); // see http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-metadata.html // This seems very much not EC2-like to hardwire an IP address for getting instance metadata, // but that's how it's done. get.setURI(new URI("http://169.254.169.254/latest/meta-data/instance-type")); get.setConfig(RequestConfig.custom() .setConnectTimeout(2000) .setSocketTimeout(2000) .build() ); HttpResponse res = httpClient.execute(get); InputStream is = res.getEntity().getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); String type = reader.readLine().trim(); reader.close(); return type; } catch (Exception e) { LOG.info("could not retrieve EC2 instance type, you may be running outside of EC2."); return null; } }
private HttpGet createGet(final String path) { final URI url = getUri(path); final HttpGet get = new HttpGet(url); get.setConfig(getRequestConfig()); return get; }
protected void configureHttpGet(HttpGet httpGet, long timeout) { httpGet.setConfig(RequestConfig.custom() .setConnectTimeout((int) timeout) .setSocketTimeout((int) timeout) .setConnectionRequestTimeout((int) timeout) .setRedirectsEnabled(true) .setCircularRedirectsAllowed(true) .setMaxRedirects(20) .setCookieSpec(CookieSpecs.STANDARD) .build() ); }
private CloseableHttpResponse executeViaProxy(Proxy proxy, String requestUrl) throws IOException { final CloseableHttpClient proxyClient; if (proxy.useAuthentication()){ proxyClient = buildHttpClient(makeCredentialsProvider(proxy.host,proxy.port,proxy.username,proxy.password)); } else { proxyClient = buildHttpClient(null); } final HttpHost proxyHttpHost = new HttpHost(proxy.host, proxy.port); final RequestConfig requestConfig = RequestConfig.custom().setProxy(proxyHttpHost).build(); final HttpGet request = new HttpGet(requestUrl); request.setConfig(requestConfig); return proxyClient.execute(request); }
.setSocketTimeout(TIMEOUT) .build(); httpGet.setConfig(requestConfig); HttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode();
httpGet.setConfig(RequestConfig.custom().setRedirectsEnabled(false).build()); // 禁止重定向
@Override public String execute(CloseableHttpClient httpclient, HttpHost httpProxy, String uri, String queryParam) throws WxErrorException, ClientProtocolException, IOException { if (queryParam != null) { if (uri.indexOf('?') == -1) { uri += '?'; } uri += uri.endsWith("?") ? queryParam : '&' + queryParam; } HttpGet httpGet = new HttpGet(uri); if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpGet.setConfig(config); } try (CloseableHttpResponse response = httpclient.execute(httpGet)) { String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); WxError error = WxError.fromJson(responseContent); if (error.getErrorCode() != 0) { throw new WxErrorException(error); } return responseContent; } }
HttpGet httpGet = new HttpGet(url); httpGet.addHeader("Accept", "application/json"); httpGet.setConfig(requestConfig); httpResponse = httpclient.execute(httpGet); final int statusCode = httpResponse.getStatusLine().getStatusCode();
if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpGet.setConfig(config);
if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpGet.setConfig(config);
@Override public boolean sendHeartbeat() throws Exception { if (StringUtil.isEmpty(consoleHost)) { return false; } URIBuilder uriBuilder = new URIBuilder(); uriBuilder.setScheme("http").setHost(consoleHost).setPort(consolePort) .setPath("/registry/machine") .setParameter("app", AppNameUtil.getAppName()) .setParameter("v", Constants.SENTINEL_VERSION) .setParameter("version", String.valueOf(System.currentTimeMillis())) .setParameter("hostname", HostNameUtil.getHostName()) .setParameter("ip", TransportConfig.getHeartbeatClientIp()) .setParameter("port", TransportConfig.getPort()) .setParameter("pid", String.valueOf(PidUtil.getPid())); HttpGet request = new HttpGet(uriBuilder.build()); request.setConfig(requestConfig); // Send heartbeat request. CloseableHttpResponse response = client.execute(request); response.close(); return true; }
@Test public void testUnauthenticatedRedirect() throws Exception { String location = serverRunning.getBaseUrl() + "/"; HttpGet httpget = new HttpGet(location); httpget.setConfig( RequestConfig.custom().setRedirectsEnabled(false).build() ); CloseableHttpResponse response = httpclient.execute(httpget); assertEquals(FOUND.value(), response.getStatusLine().getStatusCode()); location = response.getFirstHeader("Location").getValue(); response.close(); httpget.completed(); assertTrue(location.contains("/login")); }
if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpGet.setConfig(config);
if (httpProxy != null) { RequestConfig config = RequestConfig.custom().setProxy(httpProxy).build(); httpGet.setConfig(config);
get.setConfig(requestConfigBuilder.build());
public HttpGet createHttpGet(URL url) { HttpGet httpGet = new HttpGet(url.toString()); httpGet.addHeader("User-Agent", "Mozilla/5.0"); httpGet.addHeader("Connection", "keep-alive"); int timeout = (int) SECONDS.toMillis(config.getTimeout()); RequestConfig requestConfig = custom().setCookieSpec(STANDARD) .setSocketTimeout(timeout).build(); httpGet.setConfig(requestConfig); return httpGet; }
public String get(String url) { HttpGet httpGet = new HttpGet(url); httpGet.setConfig(requestConfig); try (CloseableHttpResponse response = httpClient.execute(httpGet)) { StatusLine statusLine = response.getStatusLine(); HttpEntity entity = response.getEntity(); if (statusLine.getStatusCode() >= 300) { EntityUtils.consume(entity); throw new WxRuntimeException(statusLine.getStatusCode(), statusLine.getReasonPhrase()); } String responseContent = entity == null ? null : EntityUtils.toString(entity, Consts.UTF_8); return responseContent; } catch (IOException ex) { logger.error("http get: {} failed.", url, ex); throw new WxRuntimeException(999, ex.getMessage()); } }
JsonObject httpGet(String url, User registrar, Map<String, String> queryMap) throws Exception { String getURL = getURL(url, queryMap); String authHTTPCert = getHTTPAuthCertificate(registrar.getEnrollment(), "GET", getURL, ""); HttpGet httpGet = new HttpGet(getURL); httpGet.setConfig(getRequestConfig()); logger.debug(format("httpGet %s, authHTTPCert: %s", url, authHTTPCert)); final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create(); if (registry != null) { httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(registry)); } HttpClient client = httpClientBuilder.build(); final HttpClientContext context = HttpClientContext.create(); httpGet.addHeader("Authorization", authHTTPCert); HttpResponse response = client.execute(httpGet, context); return getResult(response, "", "GET"); }