URL url = new URL("http", host, port, "/schemas/" + schemaName); GetMethod httpGet = new GetMethod(url.toString()); try { int responseCode = HTTP_CLIENT.executeMethod(httpGet); String response = httpGet.getResponseBodyAsString(); if (responseCode >= 400) { httpGet.releaseConnection();
public Get get(String uri, Class c) { GetMethod get = new GetMethod("http://127.0.0.1:54321/" + uri); Get res = new Get(); try { res._status = _client.executeMethod(get); if( res._status == 200 ) { Gson gson = new Gson(); res._res = gson.fromJson(new InputStreamReader(get.getResponseBodyAsStream()), c); } } catch( Exception e ) { throw new RuntimeException(e); } get.releaseConnection(); return res; }
try { getMethod = completionService.take().get(); URI uri = getMethod.getURI(); String instance = endpointsToServers.get(uri.getHost() + ":" + uri.getPort()); if (getMethod.getStatusCode() >= 300) { LOGGER.error("Server: {} returned error: {}", instance, getMethod.getStatusCode()); continue; JsonUtils.inputStreamToObject(getMethod.getResponseBodyAsStream(), TableSizeInfo.class); serverToSegmentSizeInfoListMap.put(instance, tableSizeInfo.segments); } catch (Exception e) { getMethod.releaseConnection();
GetMethod get = new GetMethod("http://httpcomponents.apache.org"); // execute method and handle any error responses. ... InputStream in = get.getResponseBodyAsStream(); // Process the data from the input stream. get.releaseConnection();
GetMethod method = new GetMethod("example.com/page"); method.setQueryString(new NameValuePair[] { new NameValuePair("key", "value") });
HttpClient client = new HttpClient(); GetMethod method = new GetMethod(url); method.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler(3, false)); int statusCode = client.executeMethod(method); System.err.println("Method failed: " + method.getStatusLine()); byte[] responseBody = method.getResponseBody(); } finally { method.releaseConnection();
@Test public void testSessionListenerInjection(@ArquillianResource URL baseURL) throws Exception { HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(new URL(baseURL, "bat?mode=session").toExternalForm()); int sc = client.executeMethod(method); assertEquals(HttpServletResponse.SC_OK, sc); } }
protected HttpMethod createMethod(final String type, final String uri, final int port) throws BigSwitchBcfApiException { String url; try { url = new URL(S_PROTOCOL, host, port, uri).toString(); } catch (MalformedURLException e) { S_LOGGER.error("Unable to build Big Switch API URL", e); throw new BigSwitchBcfApiException("Unable to build Big Switch API URL", e); } if ("post".equalsIgnoreCase(type)) { return new PostMethod(url); } else if ("get".equalsIgnoreCase(type)) { return new GetMethod(url); } else if ("delete".equalsIgnoreCase(type)) { return new DeleteMethod(url); } else if ("put".equalsIgnoreCase(type)) { return new PutMethod(url); } else { throw new BigSwitchBcfApiException("Requesting unknown method type"); } }
URL url = new URL(testUrl); host = url.getHost(); } catch (MalformedURLException e) { return FormValidation.error(Messages.ProxyConfiguration_MalformedTestUrl(testUrl)); method = new GetMethod(testUrl); method.getParams().setParameter("http.socket.timeout", DEFAULT_CONNECT_TIMEOUT_MILLIS > 0 ? DEFAULT_CONNECT_TIMEOUT_MILLIS : new Integer(30 * 1000)); HttpClient client = new HttpClient(); if (Util.fixEmptyAndTrim(name) != null && !isNoProxyHost(host, noProxyHost)) { client.getHostConfiguration().setProxy(name, port); Credentials credentials = createCredentials(userName, password); AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT); client.getState().setProxyCredentials(scope, credentials); } finally { if (method != null) { method.releaseConnection();
private static InputStream requestDoc(URL url) throws IOException { HttpClient client = new HttpClient(); ProxyManager proxyManager = new ProxyManager(); ProxyHost proxy = proxyManager.getProxyHost(url); if (proxy != null) { LOG.debug("for server " + url + " using proxy: '" + proxy.getHostName() + "'"); } else { LOG.debug("for server " + url + " not using proxy!"); } client.getHostConfiguration().setProxyHost(proxy); client.getState().setProxyCredentials(AuthScope.ANY, proxyManager.getProxyCredentials(url)); GetMethod method = new GetMethod(url.toString()); client.executeMethod(method); return method.getResponseBodyAsStream(); }
private PAOSResponse requestPAOSResponse() throws ServiceException { GetMethod method = new GetMethod(getURL().toString()); method.addRequestHeader(new Header("Accept", "application/vnd.paos+xml")); method.addRequestHeader(new Header("PAOS", "ver='urn:liberty:paos:2003-08';'urn:oasis:names:tc:SAML:2.0:profiles:SSO:ecp'")); try { m_client.executeMethod(method); InputStream responseBodyStream = method.getResponseBodyAsStream(); PAOSResponse paosResponse = PAOSResponse.createFromXMLStream(responseBodyStream); return paosResponse; } catch (Exception e) { throw new ServiceException(String.format("Could not send basic GET request to <%s>.", getURL()), ServiceException.SERVICE_ERROR, e); } finally { method.releaseConnection(); } }
@Override public HTTPResponse get(final URL url) throws IOException { GetMethod getMethod = new GetMethod(url.toExternalForm()); getMethod.setDoAuthentication(user != null && password != null); int responseCode = client.executeMethod(getMethod); if (200 != responseCode) { getMethod.releaseConnection(); throw new IOException( "Server returned HTTP error code " + responseCode + " for URL " + url.toExternalForm()); } return new HttpMethodResponse(getMethod); }
private InputStream setupInputStream(URL url, Map<String, String> headers) throws IOException { HttpClient client = new HttpClient(); String uri = url.toExternalForm(); if (LOGGER.isLoggable(Level.FINE)) LOGGER.log(Level.FINE, "URL is " + uri); HttpMethod get = new GetMethod(uri); if (MapUtils.isNotEmpty(headers)) { for (String headerName : headers.keySet()) { if (LOGGER.isLoggable(Level.FINE)) { LOGGER.log( Level.FINE, "Adding header " + headerName + " = " + headers.get(headerName)); } get.addRequestHeader(headerName, headers.get(headerName)); } } int code = client.executeMethod(get); if (code != 200) { throw new IOException("Connection returned code " + code); } return get.getResponseBodyAsStream(); } }
@Override public HTTPResponse get(final URL url) throws IOException { GetMethod getMethod = new GetMethod(url.toExternalForm()); getMethod.setDoAuthentication(user != null && password != null); if (tryGzip) { getMethod.setRequestHeader("Accept-Encoding", "gzip"); } int responseCode = executeMethod(getMethod); if (200 != responseCode) { getMethod.releaseConnection(); throw new IOException( "Server returned HTTP error code " + responseCode + " for URL " + url.toExternalForm()); } return new HttpMethodResponse(getMethod); }
public HTTPResponse issueGet(final URL baseUrl, final Map<String, String> kvp) throws IOException { if (LOGGER.isLoggable(Level.FINEST)) { LOGGER.finest("About to issue GET request to " + baseUrl.toExternalForm() + " with query parameters: " + kvp); } final String uri = createUri(baseUrl, kvp); GetMethod getRequest = new GetMethod(uri); HTTPResponse httpResponse = issueRequest(getRequest); return httpResponse; }
private void doRequest(String realm, String host, String user, String pass, String url, boolean handshake, int result) throws Exception { HttpClient client = new HttpClient(); client.getParams().setAuthenticationPreemptive(true); client.getState().setCredentials(new AuthScope(host, -1, realm), new UsernamePasswordCredentials(user, pass)); GetMethod get = new GetMethod(url); get.setDoAuthentication(handshake); try { int status = client.executeMethod(get); if (status == UNAUTHORIZED.getStatusCode() && handshake == true) { // doAuthentication = true means that if the request returns 401, // the HttpClient will resend the request with credentials status = client.executeMethod(get); } assertEquals(result, status); } finally { get.releaseConnection(); } } }
public static InputStream getInputStreamFromUrl(String url, String user, String password) { try { Pair<String, Integer> hostAndPort = validateUrl(url); HttpClient httpclient = new HttpClient(new MultiThreadedHttpConnectionManager()); if ((user != null) && (password != null)) { httpclient.getParams().setAuthenticationPreemptive(true); Credentials defaultcreds = new UsernamePasswordCredentials(user, password); httpclient.getState().setCredentials(new AuthScope(hostAndPort.first(), hostAndPort.second(), AuthScope.ANY_REALM), defaultcreds); s_logger.info("Added username=" + user + ", password=" + password + "for host " + hostAndPort.first() + ":" + hostAndPort.second()); } // Execute the method. GetMethod method = new GetMethod(url); int statusCode = httpclient.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { s_logger.error("Failed to read from URL: " + url); return null; } return method.getResponseBodyAsStream(); } catch (Exception ex) { s_logger.error("Failed to read from URL: " + url); return null; } }
@Test public void testAuthenticationFailureNoContext() throws Exception { HttpClient client = new HttpClient(); client.getParams().setAuthenticationPreemptive(true); GetMethod get = new GetMethod(getUrl()); get.setDoAuthentication(false); try { int status = client.executeMethod(get); assertThat(status, is(UNAUTHORIZED.getStatusCode())); assertThat(get.getResponseBodyAsString(), containsString("no security context on the session. Authentication denied on connector")); } finally { get.releaseConnection(); } }
try { String url = buildUrl(buildParams(command, params), region); HttpClient client = new HttpClient(); HttpMethod method = new GetMethod(url); if (client.executeMethod(method) == 200) { InputStream is = method.getResponseBodyAsStream(); XStream xstream = new XStream(new DomDriver()); xstream.alias("useraccount", UserAccountVO.class);
private static int httpNotification(String uri) throws IOException { URI url = new URI(uri, false); HttpClient m_client = new HttpClient(); HttpMethod method = new GetMethod(url.getEscapedURI()); method.setRequestHeader("Accept", "*/*"); return m_client.executeMethod(method); }