HttpGet someHttpGet = new HttpGet("http://google.de"); URI uri = new URIBuilder(someHttpGet.getURI()).addParameter("q", "That was easy!").build(); ((HttpRequestBase) someHttpGet).setURI(uri);
public class HttpClientTest { public static void main(String[] args) throws URISyntaxException { List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("q", "httpclient")); qparams.add(new BasicNameValuePair("btnG", "Google Search")); qparams.add(new BasicNameValuePair("aq", "f")); qparams.add(new BasicNameValuePair("oq", null)); URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search", URLEncodedUtils.format(qparams, "UTF-8"), null); HttpGet httpget = new HttpGet(uri); System.out.println(httpget.getURI()); //http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq= } }
private <T> T execute(final HttpGet get, final Class<T> entityClass) throws IOException { get.setHeader("Accept", "application/json"); final String responseMessage = execute(get); final ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); try { return mapper.readValue(responseMessage, entityClass); } catch (JsonParseException e) { final String msg = "Failed to parse Json. The specified URL " + baseUrl + " is not a proper remote NiFi endpoint for Site-to-Site communication."; logger.warn("{} requestedUrl={}, response={}", msg, get.getURI(), responseMessage); throw new IOException(msg, e); } }
// Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // Pass local context as a parameter HttpResponse response = httpclient.execute(httpget, localContext);
private static String buildUrl(String path, List<NameValuePair> qparams) throws RestApiClientException { URIBuilder builder = new URIBuilder(); builder.setPath(path); ListIterator<NameValuePair> i = qparams.listIterator(); while (i.hasNext()) { NameValuePair keyValue = i.next(); builder.setParameter(keyValue.getName(), keyValue.getValue()); } URI uri; try { uri = builder.build(); } catch (Exception e) { throw new RestApiClientException("Failed to build url; error - " + e.getMessage(), e); } return new HttpGet(uri).getURI().toString(); }
/** * Sends a request to the server and returns a custom response object with the result * * @param storeRespCookie if true, retain the value of a "Set-Cookie" header returned in the * response. */ private Response doRequest(HttpGet req, boolean storeRespCookie) throws IOException { addCookieHeader(req); CloseableHttpResponse resp = httpclient.execute(req, context); boolean isNew = true; String reqCookie = getCookieHeader(resp); if (reqCookie == null) { isNew = false; reqCookie = this.cookie; } else if (storeRespCookie) { this.cookie = reqCookie; } StatusLine status = resp.getStatusLine(); if (status.getStatusCode() != 200) { throw new IOException("Http request to " + req.getURI().getHost() + "[" + req.getURI().getPort() + "] failed. " + status); } Response response = new Response(reqCookie, EntityUtils.toString(resp.getEntity()), isNew); resp.close(); return response; }
private void addCookieHeader(HttpGet req) { // Set the cookie header if (cookie != null) { BasicClientCookie cookie = new BasicClientCookie("JSESSIONID", this.cookie); cookie.setDomain(req.getURI().getHost()); cookie.setPath("/"); BasicCookieStore cookieStore = new BasicCookieStore(); cookieStore.addCookie(cookie); context.setAttribute(HttpClientContext.COOKIE_STORE, cookieStore); } }
((HttpCommunicationsSession)peer.getCommunicationsSession()).setDataTransferUrl(get.getURI().toString());
httpget2.setHeader("User-Agent", userAgent); log.finer("Executing " + httpget2.getURI()); HttpClient httpclient2 = new DefaultHttpClient(); HttpResponse response2 = httpclient2.execute(httpget2);
@Test( expected = KettleException.class ) public void testExecService() throws Exception { HttpGet httpGetMock = mock( HttpGet.class ); URI uriMock = new URI( "fake" ); doReturn( uriMock ).when( httpGetMock ).getURI(); doReturn( httpGetMock ).when( slaveServer ).buildExecuteServiceMethod( anyString(), anyMapOf( String.class, String.class ) ); slaveServer.setHostname( "hostNameStub" ); slaveServer.setUsername( "userNAmeStub" ); slaveServer.execService( "wrong_app_name" ); fail( "Incorrect connection details had been used, but no exception was thrown" ); }
throw new KettleException( String.format( "HTTP Status %d - %s - %s", statusCode, method.getURI().toString(), statusLine.getReasonPhrase() ) );
URI uri = new URIBuilder() .setScheme("http") .setHost("www.google.com") .setPath("/search") .setParameter("q", "httpclient") .setParameter("btnG", "Google Search") .setParameter("aq", "f") .setParameter("oq", "") .build(); HttpGet httpget = new HttpGet(uri); System.out.println(httpget.getURI());
URIBuilder builder = new URIBuilder(); builder.setScheme("http").setHost("www.google.com").setPath("/search") .setParameter("q", "httpclient") .setParameter("btnG", "Google Search") .setParameter("aq", "f") .setParameter("oq", ""); URI uri = builder.build(); HttpGet httpget = new HttpGet(uri); System.out.println(httpget.getURI());
HttpGet request = new HttpGet("http://example.com/?var=1&var=2"); URIBuilder newBuilder = new URIBuilder(request.getURI()); List<NameValuePair> params = newBuilder.getQueryParams();
throw new IOException(String.format("Total Stellar REST request time to %s exceeded the configured timeout of %d ms.", httpGet.getURI().toString(), restConfig.getTimeout())); } else { throw e; throw new IOException(String.format("Stellar REST request to %s expected status code to be one of %s but " + "failed with http status code %d: %s", httpGet.getURI().toString(), restConfig.getResponseCodesAllowed().toString(), statusCode,
log.finer("Executing " + httpget2.getURI()); HttpClient httpclient2 = new DefaultHttpClient(); HttpResponse response2 = httpclient2.execute(httpget2);
List<NameValuePair> qparams = new ArrayList<NameValuePair>(); qparams.add(new BasicNameValuePair("q", "httpclient")); qparams.add(new BasicNameValuePair("btnG", "Google Search")); qparams.add(new BasicNameValuePair("aq", "f")); qparams.add(new BasicNameValuePair("oq", null)); URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search", URLEncodedUtils.format(qparams, "UTF-8"), null); HttpGet httpget = new HttpGet(uri); System.out.println(httpget.getURI());
URIBuilder builder = new URIBuilder(); builder.setScheme("http").setHost("www.google.com").setPath("/search") .setParameter("q", "httpclient") .setParameter("btnG", "Google Search") .setParameter("aq", "f") .setParameter("oq", ""); URI uri = builder.build(); HttpGet httpget = new HttpGet(uri); System.out.println(httpget.getURI());
// Create a local instance of cookie store CookieStore cookieStore = new BasicCookieStore(); // Create local HTTP context HttpContext localContext = new BasicHttpContext(); // Bind custom cookie store to the local context localContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore); HttpGet httpget = new HttpGet("http://www.google.com/"); System.out.println("executing request " + httpget.getURI()); // Pass local context as a parameter HttpResponse response = httpclient.execute(httpget, localContext);
public HostCredentials onPreAuthorization(final HttpGet req) { final String host = req.getURI().getHost(); final HostCredentials cred = credentials.get(host); final State state = cred != null ? cred.getState() : null; if (state == State.AUTH_ENTERED || state == State.AUTH_SENT) { cred.setState(State.AUTH_ENTERED, State.AUTH_SENT); return cred; } return null; }