@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); }
@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); }
@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); }
client = new HttpClient(); GetMethod get = new GetMethod(URL); get.setDoAuthentication(true); try{ HttpResponse response = client.executeMethod(get); //parsing XML from response //System.out.println(get.getResponseBodyAsString()); int statusCode = httpResponse.getStatusLine().getStatusCode(); if(statusCode == 200){ System.out.println(); // here I need HttpResponse object to get HttpEntity BufferedReader buffer = new BufferedReader (new InputStreamReader(response.getEntity().getContent())); String line = ""; while ((line = buffer.readLine()) != null) { textView.append(line); } } } finally { get.releaseConnection(); }
AuthPolicy.registerAuthScheme(AuthPolicy.NTLM, org.xyz.JCIFS_NTLMScheme.class); HttpClient client = new HttpClient(); client.getState().setCredentials(AuthScope.ANY, new NTCredentials(userName, password, "", strDomain)); GetMethod get = new GetMethod(strImageFile); get.setDoAuthentication(true); client.executeMethod(get);
private GetMethod execDiffMethod(ReviewboardConnection con, HttpClient http, String url) throws IOException { ensureAuthentication(con, http); String diffUrl = con.buildApiUrl(url, "diffs"); Response d = getResponse(http, diffUrl, Response.class); if (d.count < 1) throw new RuntimeException("Review " + url + " has no diffs"); // String diffUrl = url.concat("diff/raw/"); GetMethod diff = new GetMethod(diffUrl + d.count + "/"); diff.setDoAuthentication(true); diff.setRequestHeader("Accept", "text/x-patch"); http.executeMethod(diff); return diff; }
private static <T> T getResponse(HttpClient http, String requestUrl, Class<T> clazz) { GetMethod request = new GetMethod(requestUrl); int code; try { request.setDoAuthentication(true); request.setRequestHeader("Accept", "application/xml"); code = http.executeMethod(request); if (code == 200) { InputStream res = request.getResponseBodyAsStream(); JAXBContext jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); InputStreamReader reader = new InputStreamReader(res); return clazz.cast(unmarshaller.unmarshal(reader)); } } catch (Exception e) { throw new RuntimeException(e); } finally { request.releaseConnection(); } throw new RuntimeException("Accessing the URL " + requestUrl + " failed with code " + code); }
@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); }
private static int ensureAuthentication(ReviewboardConnection con, HttpClient http, boolean withRetry) throws IOException { initializeAuthentication(con, http); GetMethod url = new GetMethod(con.getReviewboardURL() + "api/session/"); try { url.setDoAuthentication(true); int res = http.executeMethod(url); return res; } catch (IOException e) { //trying to recover from failure... if (withRetry) return retryAuthentication(con, http); else throw e; } finally { url.releaseConnection(); } }
private GetMethod doGet(URL url, int timeout) throws IOException { HttpClient client = getClient(); client.setTimeout(timeout); GetMethod get = new GetMethod(normalizeToString(url)); get.setDoAuthentication(useAuthentication(url) || useProxyAuthentication()); get.setRequestHeader("Accept-Encoding", "gzip,deflate"); client.executeMethod(get); return get; }
try { HttpClient client = new HttpClient( new MultiThreadedHttpConnectionManager()); client.getParams().setAuthenticationPreemptive(true); Credentials credentials = new UsernamePasswordCredentials("username", "password"); client.getState().setCredentials(AuthScope.ANY, credentials); List<String> authPrefs = new ArrayList<String>(2); authPrefs.add(AuthPolicy.DIGEST); authPrefs.add(AuthPolicy.BASIC); client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs); GetMethod getMethod = new GetMethod("your_url"); getMethod.setRequestHeader("Accept", "application/xml"); client.executeMethod(getMethod); int status = getMethod.getStatusCode(); getMethod.setDoAuthentication(true); System.out.println("status: " + status); if (status == HttpStatus.SC_OK) { String responseBody = getMethod.getResponseBodyAsString(); String resp = responseBody.replaceAll("\n", " "); System.out.println("RESPONSE \n" + resp); } } catch (Exception e) { e.printStackTrace(); }
HttpClient client = new HttpClient(); Credentials creds = new UsernamePasswordCredentials(username, password); client.getState().setCredentials(new AuthScope(host, port, realmName), creds); GetMethod get = new GetMethod(url); get.setDoAuthentication(true); client.getParams().setAuthenticationPreemptive(true); // seems to be necessary in most cases client.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, Collections.singleton(AuthPolicy.DIGEST));//need to register DIGEST scheme not the basic client.getAuthSchemes().register(AuthPolicy.DIGEST, new DigestSchemeFactory()); client.executeMethod(get); result = get.getResponseBodyAsString();
/** * Builds the HTTP GET method used to fetch the metadata. The returned method advertises support for GZIP and * deflate compression, enables conditional GETs if the cached metadata came with either an ETag or Last-Modified * information, and sets up basic authentication if such is configured. * * @return the constructed GET method */ protected GetMethod buildGetMethod() { GetMethod getMethod = new GetMethod(getMetadataURI()); getMethod.addRequestHeader("Connection", "close"); getMethod.setRequestHeader("Accept-Encoding", "gzip,deflate"); if (cachedMetadataETag != null) { getMethod.setRequestHeader("If-None-Match", cachedMetadataETag); } if (cachedMetadataLastModified != null) { getMethod.setRequestHeader("If-Modified-Since", cachedMetadataLastModified); } if (httpClient.getState().getCredentials(authScope) != null) { log.debug("Using BASIC authentication when retrieving metadata from '{}", metadataURI); getMethod.setDoAuthentication(true); } return getMethod; }
public byte[] getURLContentAsBytes(String surl, String username, String password, int timeout, String userAgent) throws IOException { HttpClient client = getHttpClient(timeout, userAgent); // pass our credentials to HttpClient, they will only be used for // authenticating to servers with realm "realm", to authenticate agains // an arbitrary realm change this to null. client.getState().setCredentials(new AuthScope(null, -1, null), new UsernamePasswordCredentials(username, password)); // create a GET method that reads a file over HTTPS, we're assuming // that this file requires basic authentication using the realm above. GetMethod get = new GetMethod(surl); try { // Tell the GET method to automatically handle authentication. The // method will use any appropriate credentials to handle basic // authentication requests. Setting this value to false will cause // any request for authentication to return with a status of 401. // It will then be up to the client to handle the authentication. get.setDoAuthentication(true); // execute the GET client.executeMethod(get); // print the status and response return get.getResponseBody(); } finally { // release any connection resources used by the method get.releaseConnection(); } }
public String getURLContent(String surl, String username, String password, int timeout, String userAgent) throws IOException { HttpClient client = getHttpClient(timeout, userAgent); // pass our credentials to HttpClient, they will only be used for // authenticating to servers with realm "realm", to authenticate agains // an arbitrary realm change this to null. client.getState().setCredentials(new AuthScope(null, -1, null), new UsernamePasswordCredentials(username, password)); // create a GET method that reads a file over HTTPS, we're assuming // that this file requires basic authentication using the realm above. GetMethod get = new GetMethod(surl); try { // Tell the GET method to automatically handle authentication. The // method will use any appropriate credentials to handle basic // authentication requests. Setting this value to false will cause // any request for authentication to return with a status of 401. // It will then be up to the client to handle the authentication. get.setDoAuthentication(true); // execute the GET client.executeMethod(get); // print the status and response return get.getResponseBodyAsString(); } finally { // release any connection resources used by the method get.releaseConnection(); } }
@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(); } }
@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(); } }
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(); } } }
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(); } } }
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); 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(); } }